我需要扫描一个文件系统的文件列表,并记录那些不存在的。目前我有一个输入文件,其中有1300万个文件需要调查。此脚本需要从远程位置运行,因为我没有权限/无法直接在存储服务器上运行脚本。
我目前的方法可行,但相对较慢。我对Python还是相当陌生的,所以我在寻找加快速度的技巧。
import sys,os
from pz import padZero #prepends 0's to string until desired length
output = open('./out.txt', 'w')
input = open('./in.txt', 'r')
rootPath = '\\\\server\share\' #UNC path to storage
for ifid in input:
ifid = padZero(str(ifid)[:-1], 8) #extracts/formats fileName
dir = padZero(str(ifid)[:-3], 5) #exracts/formats the directory containing the file
fPath = rootPath + '\\' + dir + '\\' + ifid + '.tif'
try:
size = os.path.getsize(fPath) #don't actually need size, better approach?
except:
output.write(ifid+'\n')
谢谢。
您将受到I/O的限制,尤其是在网络上,因此您可以对脚本进行的任何更改都将导致非常小的加速,但在我的头脑中:
我真的不指望它会更快,但可以说它更容易阅读。在
其他方法可能更快。例如,如果您希望访问大多数文件,您只需从服务器中提取一个完整的递归目录列表,将其转换为Python
set()
,然后检查其中的成员资格,而不是为许多小请求访问服务器。我将把代码留作练习。。。在在我看来,padZero和字符串连接的东西需要很长时间。在
你想让它做的就是把所有的时间都花在阅读目录上,其他的很少。在
你一定要用python来做吗?我在C和C++中做过类似的事情。Java应该也不错。在
解释
首先将输入文件读入目录的字典:filename。然后,对于每个目录,列出服务器上该目录中的所有TIFF文件,并(set)从您应该拥有的文件名集合中减去这些文件名。把剩下的都打印出来。在
编辑:修正了一些愚蠢的事情。我写这篇文章的时候已经太晚了!
相关问题 更多 >
编程相关推荐