使用python按部分名称查找目录中的文件
我有一个目录,里面有几十万个文件 它们都遵循以下格式:使用python按部分名称查找目录中的文件,python,regex,glob,Python,Regex,Glob,我有一个目录,里面有几十万个文件 它们都遵循以下格式: datetime_fileid_metadata_collect.txt 具体示例如下所示: 201405052359559_0002230255_35702088_collect88.txt 我正在尝试编写一个脚本,当我提供的只是一个文件ID列表时,它会提取并复制单个文件 例如,我有一个文本文档fileids.txt,它包含以下内容 fileids.txt 0002230255 0001627237 0001023000 这是我到目
datetime_fileid_metadata_collect.txt
具体示例如下所示:
201405052359559_0002230255_35702088_collect88.txt
我正在尝试编写一个脚本,当我提供的只是一个文件ID列表时,它会提取并复制单个文件
例如,我有一个文本文档fileids.txt,它包含以下内容
fileids.txt
0002230255
0001627237
0001023000
这是我到目前为止编写的示例脚本。file1结果继续返回[]
import os
import re, glob, shutil
base_dir = 'c:/stuff/tub_0_data/'
destination = 'c:/files_goes_here'
os.chdir(base_dir)
text_file = open('c:/stuff/fileids.txt', 'r')
file_ids = text_file.readlines()
#file_ids = [stripped for stripped in (line.strip() for line in text_file.readlines()) if stripped]
for ids in file_ids:
id1 = ids.rstrip()
print 'file id = ',str(id1)
file1 = glob.glob('*' + str(id1) + '*')
print str(file1)
if file1 != []:
shutil.copy(base_dir + file1, destination)
我知道我还不能完全理解glob或正则表达式。如果我想根据文件名的特定字符串查找文件,我会在那里放什么
编辑:
glob.glob('*' + stuff '*')
用于在文件名中查找内容。问题是没有删除线空间 text\u file.readlines()读取整行,包括尾随的“\n”。试着剥掉它。以下内容将剥离换行符并删除空换行符:
file_ids = [line.strip() for line in text_file if not line.isspace()]
text\u file.readlines()读取整行,包括尾随的“\n”。试着剥掉它。以下内容将剥离换行符并删除空换行符:
file_ids = [line.strip() for line in text_file if not line.isspace()]
text\u file.readlines()读取整行,包括尾随的“\n”。试着剥掉它。以下内容将剥离换行符并删除空换行符:
file_ids = [line.strip() for line in text_file if not line.isspace()]
text\u file.readlines()读取整行,包括尾随的“\n”。试着剥掉它。以下内容将剥离换行符并删除空换行符:
file_ids = [line.strip() for line in text_file if not line.isspace()]
您的问题可能是linespace,它可能已经得到了回答,但我认为您可以对代码进行一些清理。诚然,我不认为有必要使用
importos
和importsys
,除非它们是更大代码的一部分
类似于下面的方法已经足够有效了
代码:
import glob
import shutil
base_dir = "C:/Downloads/TestOne/"
dest_dir = "C:/Downloads/TestTwo/"
with open("blah.txt", "rb") as ofile:
lines = [line.strip() for line in ofile.readlines()]
for line in lines:
print "File ID to Process: {}".format(line)
pattern_ = base_dir + "*" + str(line) + "*"
print pattern_
file_ = glob.glob(pattern_)
print str(file_[0])
shutil.copy(file_[0], dest_dir)
print "{} copied.".format(file_[0])
File ID to Process: 123456
C:/Downloads/TestOne/*123456*
C:/Downloads/TestOne\foobar_123456_spam.txt
C:/Downloads/TestOne\foobar_123456_spam.txt copied.
[Finished in 0.4s]
输出:
import glob
import shutil
base_dir = "C:/Downloads/TestOne/"
dest_dir = "C:/Downloads/TestTwo/"
with open("blah.txt", "rb") as ofile:
lines = [line.strip() for line in ofile.readlines()]
for line in lines:
print "File ID to Process: {}".format(line)
pattern_ = base_dir + "*" + str(line) + "*"
print pattern_
file_ = glob.glob(pattern_)
print str(file_[0])
shutil.copy(file_[0], dest_dir)
print "{} copied.".format(file_[0])
File ID to Process: 123456
C:/Downloads/TestOne/*123456*
C:/Downloads/TestOne\foobar_123456_spam.txt
C:/Downloads/TestOne\foobar_123456_spam.txt copied.
[Finished in 0.4s]
glob
是一项相当昂贵的操作。最好在开始时列出文件,然后进行匹配,在匹配时进行复制。希望这有帮助。您的问题可能是linespace,它可能已经得到了回答,但我认为您可以对代码进行一些清理。诚然,我不认为有必要使用importos
和importsys
,除非它们是更大代码的一部分
类似于下面的方法已经足够有效了
代码:
import glob
import shutil
base_dir = "C:/Downloads/TestOne/"
dest_dir = "C:/Downloads/TestTwo/"
with open("blah.txt", "rb") as ofile:
lines = [line.strip() for line in ofile.readlines()]
for line in lines:
print "File ID to Process: {}".format(line)
pattern_ = base_dir + "*" + str(line) + "*"
print pattern_
file_ = glob.glob(pattern_)
print str(file_[0])
shutil.copy(file_[0], dest_dir)
print "{} copied.".format(file_[0])
File ID to Process: 123456
C:/Downloads/TestOne/*123456*
C:/Downloads/TestOne\foobar_123456_spam.txt
C:/Downloads/TestOne\foobar_123456_spam.txt copied.
[Finished in 0.4s]
输出:
import glob
import shutil
base_dir = "C:/Downloads/TestOne/"
dest_dir = "C:/Downloads/TestTwo/"
with open("blah.txt", "rb") as ofile:
lines = [line.strip() for line in ofile.readlines()]
for line in lines:
print "File ID to Process: {}".format(line)
pattern_ = base_dir + "*" + str(line) + "*"
print pattern_
file_ = glob.glob(pattern_)
print str(file_[0])
shutil.copy(file_[0], dest_dir)
print "{} copied.".format(file_[0])
File ID to Process: 123456
C:/Downloads/TestOne/*123456*
C:/Downloads/TestOne\foobar_123456_spam.txt
C:/Downloads/TestOne\foobar_123456_spam.txt copied.
[Finished in 0.4s]
glob
是一项相当昂贵的操作。最好在开始时列出文件,然后进行匹配,在匹配时进行复制。希望这有帮助。您的问题可能是linespace,它可能已经得到了回答,但我认为您可以对代码进行一些清理。诚然,我不认为有必要使用importos
和importsys
,除非它们是更大代码的一部分
类似于下面的方法已经足够有效了
代码:
import glob
import shutil
base_dir = "C:/Downloads/TestOne/"
dest_dir = "C:/Downloads/TestTwo/"
with open("blah.txt", "rb") as ofile:
lines = [line.strip() for line in ofile.readlines()]
for line in lines:
print "File ID to Process: {}".format(line)
pattern_ = base_dir + "*" + str(line) + "*"
print pattern_
file_ = glob.glob(pattern_)
print str(file_[0])
shutil.copy(file_[0], dest_dir)
print "{} copied.".format(file_[0])
File ID to Process: 123456
C:/Downloads/TestOne/*123456*
C:/Downloads/TestOne\foobar_123456_spam.txt
C:/Downloads/TestOne\foobar_123456_spam.txt copied.
[Finished in 0.4s]
输出:
import glob
import shutil
base_dir = "C:/Downloads/TestOne/"
dest_dir = "C:/Downloads/TestTwo/"
with open("blah.txt", "rb") as ofile:
lines = [line.strip() for line in ofile.readlines()]
for line in lines:
print "File ID to Process: {}".format(line)
pattern_ = base_dir + "*" + str(line) + "*"
print pattern_
file_ = glob.glob(pattern_)
print str(file_[0])
shutil.copy(file_[0], dest_dir)
print "{} copied.".format(file_[0])
File ID to Process: 123456
C:/Downloads/TestOne/*123456*
C:/Downloads/TestOne\foobar_123456_spam.txt
C:/Downloads/TestOne\foobar_123456_spam.txt copied.
[Finished in 0.4s]
glob
是一项相当昂贵的操作。最好在开始时列出文件,然后进行匹配,在匹配时进行复制。希望这有帮助。您的问题可能是linespace,它可能已经得到了回答,但我认为您可以对代码进行一些清理。诚然,我不认为有必要使用importos
和importsys
,除非它们是更大代码的一部分
类似于下面的方法已经足够有效了
代码:
import glob
import shutil
base_dir = "C:/Downloads/TestOne/"
dest_dir = "C:/Downloads/TestTwo/"
with open("blah.txt", "rb") as ofile:
lines = [line.strip() for line in ofile.readlines()]
for line in lines:
print "File ID to Process: {}".format(line)
pattern_ = base_dir + "*" + str(line) + "*"
print pattern_
file_ = glob.glob(pattern_)
print str(file_[0])
shutil.copy(file_[0], dest_dir)
print "{} copied.".format(file_[0])
File ID to Process: 123456
C:/Downloads/TestOne/*123456*
C:/Downloads/TestOne\foobar_123456_spam.txt
C:/Downloads/TestOne\foobar_123456_spam.txt copied.
[Finished in 0.4s]
输出:
import glob
import shutil
base_dir = "C:/Downloads/TestOne/"
dest_dir = "C:/Downloads/TestTwo/"
with open("blah.txt", "rb") as ofile:
lines = [line.strip() for line in ofile.readlines()]
for line in lines:
print "File ID to Process: {}".format(line)
pattern_ = base_dir + "*" + str(line) + "*"
print pattern_
file_ = glob.glob(pattern_)
print str(file_[0])
shutil.copy(file_[0], dest_dir)
print "{} copied.".format(file_[0])
File ID to Process: 123456
C:/Downloads/TestOne/*123456*
C:/Downloads/TestOne\foobar_123456_spam.txt
C:/Downloads/TestOne\foobar_123456_spam.txt copied.
[Finished in 0.4s]
glob
是一项相当昂贵的操作。最好在开始时列出文件,然后进行匹配,在匹配时进行复制。希望这有帮助。将此行:file\u id=text.file.readlines()
更改为file\u id=text\u file.readlines()
,然后再次运行。该\uuu
上的打字错误可能是一个问题。已修复,它实际上是一个更大脚本的一部分,并且打字错误不存在于原始代码中。我刚刚为我的问题重写了核心代码。glob.glob就是我的问题所在。将这一行:file\u id=text.file.readlines()
更改为file\u id=text\u file.readlines()
,然后再次运行它。该\uuu
上的打字错误可能是一个问题。已修复,它实际上是一个更大脚本的一部分,并且打字错误不存在于原始代码中。我刚刚为我的问题重写了核心代码。glob.glob就是我的问题所在。将这一行:file\u id=text.file.readlines()
更改为file\u id=text\u file.readlines()
,然后再次运行它。该\uuu
上的打字错误可能是一个问题。已修复,它实际上是一个更大脚本的一部分,并且打字错误不存在于原始代码中。我刚刚为我的问题重写了核心代码。glob.glob就是我的问题所在。将这一行:file\u id=text.file.readlines()
更改为file\u id=text\u file.readlines()
,然后再次运行它。该\uuu
上的打字错误可能是一个问题。已修复,它实际上是一个更大脚本的一部分,并且打字错误不存在于原始代码中。我刚刚为我的问题重写了核心代码。glob.glob是我的问题所在。这不是在空闲状态下工作。“for line in text_file,readlies()”处的逗号导致了一个错误。@alienarchist-是的,我刚刚注意到。这应该是一个句点。修复了它。如果不在内部循环上使用line.isspace(),可能会更好,而不是重复它两次。@alienarchist-我假设glob.glob(“*”)列出了一组文件以及由print'file id=',str(id)打印的内容是你想要的……,如果你到命令行并输入了代码> dir c:\FielsGoeSuth[\dIdEdTys**/Case>,你得到你想要的文件] @ ExalnAcGrace-另一个要考虑的……因为这是一个巨大的目录列表,你可以用OS.ListDIR()读取一次,然后只过滤每个ID的列表。(甚至可能拆分文件名的id部分)