如何根据Excel中的列表找到并移动某些文件? [英] How do I find and move certain files based on a list in excel?

查看:471
本文介绍了如何根据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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆