2013-11-26 200 views
2

我想刪除一個文件夾及其內容,然後創建另一個空的文件,這裏是我與我的程序開始相處的錯誤。文件夾刪除錯誤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' 

回答

1

試着用開放( 「DATA/Players.txt」 或 「DATA/Strengths.txt」 或 「DATA/Skills.txt」)改變 : 到 開放(「DATA /播放器。 txt「或」DATA/Strengths.txt「或」DATA/Skills.txt「)作爲f: f.close()

+1

這沒有任何意義 - 上下文管理器確保文件在退出塊時關閉 - 在此塊內部具有'f.close()')沒有任何價值。 –

0

這裏的問題是,您打開的文件在您要刪除的目錄 - 他們然後被鎖定並且意味着文件夾不能被刪除。

你似乎認爲with open("DATA/Players.txt" or "DATA/Strengths.txt" or "DATA/Skills.txt"):將檢查這些文件的存在,它根本不會做。

"DATA/Players.txt" or "DATA/Strengths.txt" or "DATA/Skills.txt"將評估爲"DATA/Players.txt"or返回第一True值,和非空字符串是True),其然後將被打開。

你可能想要的是if any(os.path.exists(path) for path in ("DATA/Players.txt", "DATA/Strengths.txt", "DATA/Skills.txt")):

你以後的誤差可能是由DATA是一個目錄造成的 - 這意味着open("DATA")沒有意義(open()打開文件)。

相關問題