Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 文件夹删除错误shutil.rmtree,另一进程正在使用该文件_Python 3.x - Fatal编程技术网

Python 3.x 文件夹删除错误shutil.rmtree,另一进程正在使用该文件

Python 3.x 文件夹删除错误shutil.rmtree,另一进程正在使用该文件,python-3.x,Python 3.x,我试图删除一个文件夹及其内容,然后创建另一个空文件夹,下面是我在程序开始时遇到的错误 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:\Ch

我试图删除一个文件夹及其内容,然后创建另一个空文件夹,下面是我在程序开始时遇到的错误

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'
救命啊

更新

我换了

with open("DATA/Players.txt" or "DATA/Strengths.txt" or "DATA/Skills.txt"):

我运行了这个程序(数据在数据文件夹中),它运行得很好,并且替换了数据文件夹,但是在替换数据文件夹之后,我就得到了这个错误

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'
玩家已经创建! 覆盖老玩家? 对 您确定要覆盖旧玩家吗? 数据文件夹中的所有数据都将被覆盖! 对 数据文件夹已被覆盖

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'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Tristan\Desktop\Character's attributes\Character's attributes.py”,第35行,在
开放(“数据”):
PermissionError:[Errno 13]权限被拒绝:“数据”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:\Users\Tristan\Desktop\Character's attributes\Character's attributes.py”,第39行,在
os.makedirs(“数据”)
makedirs中的文件“C:\Python33\lib\os.py”,第269行
mkdir(名称、模式)
FileExistError:[WinError 183]无法在文件已存在时创建该文件:“数据”
当我在数据文件夹中没有数据的情况下运行它时,我得到了这个错误

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'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Tristan\Desktop\Character's attributes\Character's attributes.py”,第35行,在
开放(“数据”):
PermissionError:[Errno 13]权限被拒绝:“数据”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“C:\Users\Tristan\Desktop\Character's attributes\Character's attributes.py”,第39行,在
os.makedirs(“数据”)
makedirs中的文件“C:\Python33\lib\os.py”,第269行
mkdir(名称、模式)
FileExistError:[WinError 183]无法在文件已存在时创建该文件:“数据”
尝试更改:

with open("DATA/Players.txt" or "DATA/Strengths.txt" or "DATA/Skills.txt"):
致:


这里的问题是,您正在试图删除的目录中打开文件-它们随后被锁定,意味着无法删除该文件夹

您似乎认为
使用open(“DATA/Players.txt”或“DATA/Strengths.txt”或“DATA/Skills.txt”):
将检查这些文件的存在,但它根本不会这样做

“DATA/Players.txt”或“DATA/Strengths.txt”或“DATA/Skills.txt”
将计算为
“DATA/Players.txt”
返回第一个
True
值,非空字符串为
True
),然后打开

您可能想要的是
如果有的话(在(“DATA/Players.txt”、“DATA/Strengths.txt”、“DATA/Skills.txt”)中的路径存在os.path(path)):


后面的错误可能是由于
数据
是一个目录导致的-这意味着
打开(“数据”)
没有意义(
打开()
打开文件)。

这没有意义-上下文管理器确保在退出块时关闭文件-在这样的块中包含
f.close()
)没有任何值。
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()