Reputation: 25
I get winErrors at runtime like 'The system cannot find the file specified', but I know that those files exist... The idea is to try and use recurssion to embed itself into every file, and then delete them, which greatly decreases the time spent deleting the files. My friend made this in Java, and it managed to delete 3GBs in 11 seconds. I wanted to use the same idea with Python and this is the result.
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()
Any ideas will be much appreciated. I am using Python 3.3 on Windows 7 64-bit
Upvotes: 2
Views: 330
Reputation: 9884
Sounds like you just need:
import shutil
path = input("Enter path to delete")
shutil.rmtree(path);
Upvotes: 2
Reputation: 414565
os.listdir()
returns relative paths. Use full path os.remove(os.path.join(pathDir, folder))
.
Upvotes: 2