我正在尝试遍历目录。下面是代码:
file_list = []
os.chdir(self.config.Root_Directory_Path())
for root, dirs, files in os.walk("."):
file_list.extend( join(root,f) for f in files )
file_sorted = sorted(file_list)
f = open(self.config.Client_Local_Status(),'wb')
for file in file_sorted:
print(file + "|" + str(os.path.getmtime(file)) + "\n")
f.close()
首先,我遍历树,然后对其进行排序,然后打印。但是我在遍历时收到以下错误。我非常确定该文件存在,但无法找出错误的原因。请帮助我找出错误的原因并修复它。
下面是输出。
输出:
.\Drivers\Intel Drivers\Applications\Software\Applications\Wave_Embassy_Trust_Suite\EMBASSY Security Center\program files\Wave Systems Corp\EMBASSY Security Center\plugins\cpm.scp\webinterface\ru\js\HelpMessages.js|1229488128.0
.\Drivers\Intel Drivers\Applications\Software\Applications\Wave_Embassy_Trust_Suite\EMBASSY Security Center\program files\Wave Systems Corp\EMBASSY Security Center\plugins\cpm.scp\webinterface\ru\js\Strings.js|1229488128.0
成功打印大量文件名后,一个特定文件的代码失败,如下所示:
错误:
Traceback (most recent call last):
File "C:\SyncClientRK\SyncClientRK.py", line 183, in <module>
SyncClientRK()
File "C:\SyncClientRK\SyncClientRK.py", line 17, in __init__
self.getStatus()
File "C:\SyncClientRK\SyncClientRK.py", line 38, in getStatus
self.generateLocalStatus()
File "C:\SyncClientRK\SyncClientRK.py", line 53, in generateLocalStatus
print(file + "|" + str(os.path.getmtime(file)) + "\n")
File "C:\Python33\lib\genericpath.py", line 54, in getmtime
return os.stat(filename).st_mtime
FileNotFoundError: [WinError 3] The system cannot find the path specified: '.\\Drivers\\Intel Drivers\\Applications\\Software\\Applications\\Wave_Embassy_Trust_Suite\\EMBASSY Security Center\\program files\\Wave Systems Corp\\EMBASSY Security Center\\plugins\\cpm.scp\\webinterface\\zh-CHS\\AccessingToolkit.htm'
请注意,文件是在循环中获取并打印出来的,但是 os.path.getmtime 会抛出一个找不到的错误。无法理解为什么以及如何解决这个问题。