我正在尝试制作文件删除程序

时间:2013-01-11 19:31:59

标签: python windows python-3.x

我在运行时获得了winErrors,例如'系统找不到指定的文件'但我知道那些文件存在...想法是尝试使用recurssion将自己嵌入到每个文件中,并且然后删除它们,这大大减少了删除文件所花费的时间。我的朋友用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

2 个答案:

答案 0 :(得分:2)

os.listdir()返回相对路径。使用完整路径os.remove(os.path.join(pathDir, folder))

答案 1 :(得分:2)

听起来你只需要:

import shutil

path = input("Enter path to delete")

shutil.rmtree(path);