2
我试图运行两个操作:复制成千上万的文件(按名称过滤)到指定文件夹
- 含有一些标识(其导致文件名).txt文件开始,如果检查该文件在一个文件夹内;
- 如果步骤1)为true,则将该文件从该文件夹复制到指定的文件夹。
.txt文件存储代码喜欢这些:
111081
112054
112051
112064
这是我曾尝试:
from glob import glob
from shutil import copyfile
import os
input = 'C:/Users/xxxx/ids.txt'
input_folder = 'C:/Users/xxxx/input/'
dest_folder = 'C:/Users/xxxx/output/'
with open(input) as f:
for line in f:
string = "fixed_prefix_" + str(line.strip()) + '.asc'
if os.path.isfile(string):
copyfile(string, dest_folder)
的string
变量生成此(例如):
print string
fixed_prefix_111081.asc
然后,我确定搜索和将文件复制到目标文件夹还有其他问题。主要问题是我不知道如何搜索input_folder
中的fixed_prefix_111081.asc
文件。
尝试'line.strip()'删除结束line char(s) –
@ Jean-FrançoisFabre谢谢,我处理了这个问题。我编辑了这个问题。 – mgri