如何根据Excel中的列表找到并移动某些文件? [英] How do I find and move certain files based on a list in excel?
问题描述
- 000001_001_m
- 000001_002_m
- 000001_003_m
- 000001_004_m
- 000002_001_m
- 000002_002_m
- 000003_001_m
前六个字符对应于Access数据库中的唯一标识符,首先_对应于扫描项目的页码。
我正在寻找一种方法来获取csv文件中唯一标识符的列表,查找所有文件具有匹配标识符的文件夹,并将它们移动到另一个文件夹。任何有关如何执行此操作的建议?
获取标识符后,您可以通过将列表转换为一组来获得唯一的标识符。
之后,您可以使用 glob
模块查找与您的模式匹配的所有文件名( identifier _ *
)。并使用 shutil.move
将找到的文件移动到目标文件夹。
import glob
import shutil
destination_folder ='/ path_to / destination_folder /'
identifiers = ['000001','000001','000002','000002' ,'000003']
unique_identifiers = set(identifiers)
用于unique_identifiers中的标识符:
在glob.glob('%s_ *'%标识符)中的文件名:
shutil.move(filename,destination_folder)
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?
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)
这篇关于如何根据Excel中的列表找到并移动某些文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!