[英]FileExistsError: [WinError 183] Cannot create a file when that file already exists
I have a main "MAIN" folder and I would like to create several sub-folders within this called "test_1", "test_2", and so on.我有一个主“MAIN”文件夹,我想在其中创建几个子文件夹,称为“test_1”、“test_2”等。 I have done the following which successfully creates the "MAIN" folder and "test_1" within the main folder;
我已完成以下操作,成功在主文件夹中创建了“MAIN”文件夹和“test_1”; however, it fails to create the subsequent sub-folders.
但是,它无法创建后续的子文件夹。 It raises the following error:
它引发以下错误:
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'MAIN\\test_1'
I'm not sure where I'm going wrong in my logic and thus, my code.我不确定我的逻辑和我的代码哪里出错了。 I would really appreciate any assistance on this.
我非常感谢您对此提供的任何帮助。
Here's the relevant snippet of my code:这是我的代码的相关片段:
self.counter = 1
self.MAIN_folder = "MAIN"
if not os.path.exists(self.MAIN_folder):
os.makedirs(self.MAIN_folder)
self.test_folder = os.path.join(self.MAIN_folder, "test")
if not os.path.exists(self.test_folder):
os.makedirs(self.test_folder + "_" + str(self.counter))
else:
self.counter += 1
os.makedirs(self.test_folder + "_" + str(self.counter))
The problem is that you are not checking whether or not the relevant folder exists first.问题是您没有首先检查相关文件夹是否存在。 You only check
self.test_folder
which is never a folder that you create, thus your program continually attempts to create the MAIN/test_1
.您只检查
self.test_folder
这绝不是您创建的文件夹,因此您的程序会不断尝试创建MAIN/test_1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.