我在运行时遇到 winErrors,例如“系统找不到指定的文件”,但我知道这些文件存在......想法是尝试使用递归将自身嵌入到每个文件中,然后删除它们,这大大降低了删除文件所花费的时间。我的朋友用 Java 做的,它设法在 11 秒内删除了 3GB。我想在 Python 中使用相同的想法,这就是结果。
import os, sys, glob, fileinput, string
from os import *
def fileInput():
#asks for input of a file path
Folder = input("Please input a file path: ")
filePathLength = len(Folder)
#checks to make sure input was provided
if filePathLength == 0:
print("Please provide a folder...")
fileInput()
else:
#checks to make sure that it is a proper path, ie- that is has ":\\"
if Folder.find(":\\") == -1:
print("Make sure the path is valid")
fileInput()
else:
#if the path is a directory it calls the delete folder function
print("Inputted path: " + Folder)
if os.path.isdir(Folder):
deleteFolder(Folder)
else:
print("Path does not exist...")
fileInput()
def deleteFolder(pathDir):
print(str(pathDir))
try:
for folder in os.listdir(pathDir):
if folder.find(".") == -1:
deleteFolder(pathDir + "\\" + folder)
except NotADirectoryError as notADirectory:
print(str(notADirectory))
try:
for folder in os.listdir(pathDir):
if folder.find(".") != -1:
os.remove(folder)
print("deleted file " + str(folder))
except IOError as errorCheck:
print(str(errorCheck))
fileInput()
任何想法将不胜感激。我在 Windows 7 64 位上使用 Python 3.3