首页 > 解决方案 > 找不到文件错误:[WinError 2] Python 目录错误

问题描述

当我尝试从目录中删除一些文件时出现错误。当我删除文件时,我收到此错误。

Traceback (most recent call last):
  File "C:/Python37-32/deletefiles.py", line 36, in <module>
    os.unlink(os.path.join(folder_path,file))
FileNotFoundError: [WinError 2] The system cannot find the file specified:'C:/*****/*****/*****/****\\3047InforEmpData04012020082901.csv'

该程序是

path = r"C:/*****/*****/*****/"
folder_list = ["ftpuser_AMC", 
 "ftpuser_APM","ftpuser_BAL","ftpuser_BBD","ftpuser_BBW","ftpuser_BEM","ftpuser_BGM",               
 "ftpuser_gah","ftpuser_jac","ftpuser_ker","ftpuser_mer","ftpuser_mhv","ftpuser_sop","ftpuser_trg",
 "ftpuser_win","ftpuser_WSG"]

current_time = time.time()
lst_folder_list = []
if os.path.exists(path):
  for root, dirs, files in os.walk(path):
    if os.path.split(root)[-1] in folder_list:
        lst_folder_list.append(root)
else:
   print("Error Path Doesn't Exist!!!!!!!!!!!!")

# alternatively you could provide the listof folderpaths directly of course
for folder_path in lst_folder_list:

for root, dirs, files in os.walk(folder_path):
    for file in files:
        full_path = os.path.join(root,file)
        file_status = os.stat(full_path)
        #print(full_path)

        if root.endswith("EightFiles"):
            if(current_time - file_status.st_mtime) // (86400) >= 1:
                print("% s has been removed successfully 7 days" % full_path)  
                os.unlink(os.path.join(folder_path,file))
        
        if root.endswith("InforFiles"):
            if(current_time - file_status.st_mtime) // (86400) >= 7:
                print("% s has been removed successfully 7 days" % full_path)  
                os.unlink(os.path.join(folder_path,file))
        
        if root.endswith("FilesSent"):
            if(current_time - file_status.st_mtime) // (86400) >= 7:
                print("% s has been removed successfully 7 days" % full_path)  
                os.unlink(os.path.join(folder_path,file))

        if(current_time - file_status.st_mtime) // (86400) >= 21:
                print("% s has been removed successfully 21 days" % full_path)  
                os.unlink(os.path.join(folder_path,file))

标签: pythonpython-3.x

解决方案


一个文件在您的语句中可以满足 2 个条件if(例如,名称以“EightFiles”结尾并且存在 21 天),这意味着将尝试取消链接两次。解决方案是使用elif仅在所有先前的子句都没有运行时才运行的子句。

if thing:
    do work
elif other thing:
    do other work
...

这些 if 是重复代码,因此值得考虑一种消除这种情况的方法。您可以将名称/生存时间值放入列表并循环访问它们。只关注那部分,

# time to live in days when files end with various values
root_endswith_ttl = [("", 21), ("EightFiles", 1), ("InforFiles", 7),
    ("FilesSent", 7)] 

current_time = time.time()

for root, dirs, files in os.walk(folder_path):
    for file in files:
        full_path = os.path.join(root,file)
        delta = (current_time - os.stat(full_path).st_mtime)//86400
        for name, ttl in root_endswith_ttl:
            if delta >= ttl and root.endswith(name):
                os.unlink(full_path)
                print("% s has been removed successfully %i days" 
                        % (full_path, ttl))
                break

推荐阅读