How do I find and move certain files based on a li

2019-09-07 01:59发布

I have a folder with file names like this:

  • 000001_001_m
  • 000001_002_m
  • 000001_003_m
  • 000001_004_m
  • 000002_001_m
  • 000002_002_m
  • 000003_001_m

The first six characters correspond to a unique identifier in an Access database, and all content after the first _ corresponds to the page number of the scanned item.

I'm looking for a way to take a list of unique identifiers in a csv file, find all files in a folder with matching identifiers, and move them to another folder. Any advice on how to do this?

标签: python excel
1条回答
Bombasti
2楼-- · 2019-09-07 02:22

After getting the identifiers you could get unique identifiers by converting your list into a set.
After that you could use glob module to find all the filenames matching your pattern (identifier_*). And using shutil.move move found files to destination folder.

import glob
import shutil

destination_folder = '/path_to/destination_folder/'

identifiers = ['000001', '000001', '000002', '000002', '000003']
unique_identifiers = set(identifiers)

for identifier in unique_identifiers:
    for filename in glob.glob('%s_*' % identifier):
        shutil.move(filename, destination_folder)
查看更多
登录 后发表回答