請不要將其標記爲重複,因爲我查看了此錯誤的其他問題,但我仍然無法弄清楚。掃描文件夾時出現各種錯誤
我正在製作一個基本掃描程序,用於掃描指定給它的目錄,並刪除所有超過90天的子目錄。下面是代碼:
import os, sys
import time
import shutil
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
now = time.time()
home1 = os.path.join(os.environ["HOMEPATH"], "Desktop")
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
root = Tk()
root.withdraw()
path = tkFileDialog.askdirectory(initialdir=desktop, title="Select folder to scan from: ")
path = path.encode('utf-8')
for x in os.walk(path):
for folders in x:
while os.stat(folders).st_mtime < now - 90 * 86400:
q = raw_input('Folder(s) found. NOTE: This will delete all directories or subdirectories in the folder. Do you want to remove?(y/n) ')
if str(q) == "y" or str(q) == "Y":
shutil.rmtree(path, ignore_errors=True)
print 'Successfully deleted folder(s)'
elif str(q) == "n" or str(q) == "N":
print 'Folders not deleted.'
sys.exit()
else:
print 'No folder(s) over 90 days'
以下是完整的回溯:
Traceback (most recent call last):
File "C:/Users/Bill/Desktop/limitScanner/scanner.py", line 20, in <module>
while os.stat(folders).st_mtime < now - 90 * 86400:
TypeError: coercing to Unicode: need string or buffer, list found
編輯:當我使用我的程序與多個子文件夾,如桌面文件夾中的文件夾,它給了我下面的錯誤:WindowsError: [Error 2] The system cannot find the file specified
和第一個文件夾的名稱。我正在使用python 2.7.13
。任何人都可以幫忙嗎?任何幫助廣泛讚賞。
我的程序似乎凍結時這樣做 – Bill
@Bill它被卡在while循環中。您應該使用* if *塊來代替。 –