![](/img/trans.png)
[英]FileNotFoundError: [WinError 2] The system cannot find the file specified:
[英]FileNotFoundError: [WinError 2] The system cannot find the file specified
如果我对我的代码遇到的问题有帮助,将不胜感激。 该代码的目的是识别新创建的目录,以便可以将它们手动添加到磁带库进行备份。 我遇到的问题(并非总是可复制的)是在删除目录时出现的,代码崩溃了。 这是我收到的错误:
G:\Development\Python\Imaging In progress>Imaging_In_Progress.py
2015-03-02 08-41
{} Missing!
Traceback (most recent call last):
File "G:\Development\Python\Imaging In progress\Imaging_In_Progress.py", line 21, in <module>
seconds = os.path.getctime(dir)
File "C:\Python34\lib\genericpath.py", line 65, in getctime
return os.stat(filename).st_ctime
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'S:\\12345'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "G:\Development\Python\Imaging In progress\Imaging_In_Progress.py", line 27, in <module>
print("{} Missing!").format(dir)
AttributeError: 'NoneType' object has no attribute 'format'
我知道为什么会发生错误,尽管我不清楚为什么会在某些目录而不是其他目录上发生。 我确实尝试添加异常,希望如果发生FileNotFoundError,它将被忽略并且脚本将继续。 似乎不是这样,也许是由于异常的位置?
请在下面找到我的代码:
import time
import os
#Acquires current time
from datetime import datetime
current_time = str(datetime.now())
current_time = (time.strftime('%Y-%m-%d %H-%M'))
print(current_time)
OUTPUT_FILE = ("{}.txt".format(current_time))
NEW_DIRS =[]
while 1:
#Acquires list of dirs and the creation date attribute associated to them
for dir in os.listdir("S:\\"):
dir = os.path.join("S:\\", dir)
if os.path.isdir(dir):
try:
seconds = os.path.getctime(dir)
datecreated = (time.strftime('%Y-%m-%d %H:%M', time.localtime(seconds)))
except (FileNotFoundError, IOError,AttributeError):
print("{} Missing!").format(dir)
close_time = "23-59"
DATE_TIME = str(datetime.now())
DATE_TIME = (time.strftime('%H-%M'))
if DATE_TIME == close_time:
quit()
elif dir not in NEW_DIRS and datecreated > current_time:
with open(OUTPUT_FILE,"a") as c:
c.write("{}\n".format(dir))
NEW_DIRS.append(dir)
print ("{} added to array".format(dir))
在您的代码中
print("{} Missing!").format(dir)
在print(...)
返回的值print(...)
None
print(...)
上调用format
。 这就是为什么出现此错误的原因:
AttributeError: 'NoneType' object has no attribute 'format'
解决方法是调整右括号:
print("{} Missing!".format(dir))
请记住,每当您看到
During handling of the above exception, another exception occurred:
这意味着您的异常处理代码引发了自己的错误。 这可能表明except
子句中的代码错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.