我想刪除一個文件夾及其內容,然後創建另一個空的文件,這裏是我與我的程序開始相處的錯誤。文件夾刪除錯誤shutil.rmtree,另一個進程正在使用的文件
Players have already been created!
Overwrite old players?
yes
Are you sure you want to overwrite old players?
All data in DATA folder will be overwritten!
yes
Traceback (most recent call last):
File "D:\Character's attributes\Character's attributes.py", line 23, in <module>
shutil.rmtree("DATA")
File "C:\Python32\lib\shutil.py", line 283, in rmtree
onerror(os.remove, fullname, sys.exc_info())
File "C:\Python32\lib\shutil.py", line 281, in rmtree
os.remove(fullname)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'DATA\\Players.txt'
除了Python之外,沒有任何東西是開放的,而且我已經在多臺計算機上試過了。 這是我的代碼的這一部分。
Y = "Yes", "yes", "Y", "y"
N = "No", "no", "N", "n"
try:
with open("DATA/Players.txt" or "DATA/Strengths.txt" or "DATA/Skills.txt"):
print("Players have already been created!")
time.sleep(1)
print("Overwrite old players?")
answer = input()
if answer in Y:
print("Are you sure you want to overwrite old players?")
print("All data in DATA folder will be overwritten!")
answer = input()
if answer in Y:
shutil.rmtree("DATA")
os.makedirs("DATA")
print("DATA folder has been overwritten!")
elif answer in N:
print("DATA termination aborted! Phew! That was close!")
time.sleep(2)
sys.exit("Exiting...")
elif answer in N:
sys.exit("Exiting...")
except IOError:
print()
幫助!
更新
我已經取代
with open("DATA/Players.txt" or "DATA/Strengths.txt" or "DATA/Skills.txt"):
與
with open("DATA/Players.txt" or "DATA/Strengths.txt" or "DATA/Skills.txt") as f: f.close()
,我跑的程序(與數據文件夾中的數據),它工作得很好,並取代了DATA文件夾,但是在更換DATA文件夾後,我得到了這個錯誤。
玩家已經創建! 覆蓋舊玩家? 是 您確定要覆蓋舊玩家嗎? 數據文件夾中的所有數據都將被覆蓋! 是 DATA文件夾已被覆蓋!
Traceback (most recent call last):
File "C:\Users\Tristan\Desktop\Character's attributes\Character's attributes.py", line 35, in <module>
with open("DATA"):
PermissionError: [Errno 13] Permission denied: 'DATA'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Tristan\Desktop\Character's attributes\Character's attributes.py", line 39, in <module>
os.makedirs("DATA")
File "C:\Python33\lib\os.py", line 269, in makedirs
mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'DATA'
當我在DATA文件夾中沒有數據後運行它時,出現此錯誤。
Traceback (most recent call last):
File "C:\Users\Tristan\Desktop\Character's attributes\Character's attributes.py", line 35, in <module>
with open("DATA"):
PermissionError: [Errno 13] Permission denied: 'DATA'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Tristan\Desktop\Character's attributes\Character's attributes.py", line 39, in <module>
os.makedirs("DATA")
File "C:\Python33\lib\os.py", line 269, in makedirs
mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'DATA'
這沒有任何意義 - 上下文管理器確保文件在退出塊時關閉 - 在此塊內部具有'f.close()')沒有任何價值。 –