我的要求是,如果我在文件名中发现一个特定的模式,那么我需要删除属于该组的相应文件组。例如,下面是我拥有的一组文件:
file1.infile_inprogress_2015033
file1.infile_rsn_20150330022431
file1.infile_err_20150330022431
file2.infile_03_29_2015_05:08:46
file2.infile_03_29_2015_05:09:56
file3.infile_20150330023214
我需要在文件名中搜索的模式是:"inprogress"
。因此,在上面的列表中,我需要删除以下文件:
file1.infile_inprogress_2015033
file1.infile_rsn_20150330022431
file1.infile_err_20150330022431
因为上面的列表在标识符"infile"
之前有相同的文件名("file1"
)。你知道吗
到目前为止,我只能列出以下文件:
filelist = (glob.glob('C:\\CIRP\\Velocidata\\Test\\*'))
for file in filelist:
filenamecopied = os.path.basename(file)
if fnmatch.fnmatch(filenamecopied,"*Inprogress*"):
print ('Delete the group of files ')
else:
print ('skip this file')
有几个问题:
如果我假设文件名格式是一堆字母或数字,然后是“.”,然后是更多的字符,它们以随机顺序出现,我会这样做:
有点像这样:
我还没有做实际的删除代码,但你可以检查是否这是捕捉你的一切。我使用了简单的字符串函数,而不是re来捕捉基于您所说的文件名。如果没有,请回复以上问题的答案!你知道吗
OSwalk是一个更好的选择(更容易阅读),然后根据文件名进行筛选。你知道吗
您可以使用字典和各种优化,但这是最直接的。你知道吗
你需要^{} 。在文档中,
os.unlink
用于在
if
子句中添加几行相关问题 更多 >
编程相关推荐