简体   繁体   English

FileExistsError:[WinError 183] 当文件已存在时无法创建文件

[英]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.

相关问题 如何解决 FileExistsError: [WinError 183] 当文件已经存在时无法创建文件? - How to resolve FileExistsError: [WinError 183] Cannot create a file when that file already exists? 为什么我收到错误消息:“FileExistsError: [WinError 183] 当文件已存在时无法创建文件”? - Why am I getting the error: "FileExistsError: [WinError 183] Cannot create a file when that file already exists"? Winerror 183 文件已存在时无法创建文件 - Winerror 183 Cannot create a file when that file already exists 重命名txt文件。 编辑版本:[错误183]该文件已存在时无法创建该文件 - Rename txt file. Edited version: [Error 183] Cannot create a file when that file already exists Python无法创建文件错误[错误:183] - Python Cannot create a file error [Error : 183] Google App Engine:“文件已经存在时无法创建” - Google App Engine: “Cannot create a file when that file already exists” 文件存在时 os.rename 不会引发 FileExistsError - os.rename does not raise FileExistsError when file exists Python 创建目录错误 当文件已存在时无法创建文件 - Python create directory error Cannot create a file when that file already exists 使用Python观察到该文件已存在时无法创建文件 - Cannot create a file when that file already exists error observed using Python python os.rename“”在该文件已存在时无法创建该文件 - python os.rename “”cannot create a file when that file already exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM