我正在尝试删除文件夹及其内容,然后创建另一个空文件夹,这是我在程序启动时遇到的错误。
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文件夹之后,我收到了这个错误。
已经创建了玩家! 覆盖老玩家? 是 你确定要覆盖旧玩家吗? 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'
答案 0 :(得分:1)
尝试更改 with open(“DATA / Players.txt”或“DATA / Strengths.txt”或“DATA / Skills.txt”): 至 使用open(“DATA / Players.txt”或“DATA / Strengths.txt”或“DATA / Skills.txt”)作为f: f.close()
答案 1 :(得分: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()
打开文件)。