简体   繁体   English

为什么我收到错误消息:“FileExistsError: [WinError 183] 当文件已存在时无法创建文件”?

[英]Why am I getting the error: "FileExistsError: [WinError 183] Cannot create a file when that file already exists"?

Why am I getting error, if my script creates a folder?如果我的脚本创建了一个文件夹,为什么会出现错误? I am using Python on Windows 7. The error:我在 Windows 7 上使用 Python。错误:

FileExistsError: [WinError 183] Cannot create a file when that file already exists: [path to the file or folder in question] FileExistsError:[WinError 183] 当文件已存在时无法创建文件:[相关文件或文件夹的路径]

The problem is that file and folder were not there.问题是文件和文件夹不存在。

I just encountered this same issue.我刚刚遇到了同样的问题。 This thread helped me solve the problem, but the following clarification might help someone:该线程帮助我解决了问题,但以下说明可能对某人有所帮助:

For me, the misunderstanding came from shutil.copytree(source, destination, symlinks, ignore) .对我来说,误解来自shutil.copytree(source, destination, symlinks, ignore)

I read destination as being the place where my copied tree will go.我将destination视为我复制的树将去的地方。 In reality, it creates that location and then copies the tree there.实际上,它会创建该位置,然后将树复制到那里。 So if your destination directory already exists, like mine did, you'll get the error.因此,如果您的目标目录已经存在,就像我的一样,您将收到错误消息。

Hope that helps someone.希望对某人有所帮助。

As the comments point out, the folder already exists.正如评论所指出的,该文件夹已经存在。 You seem to think that attempts to create a folder that already exists should simply do nothing.您似乎认为尝试创建一个已经存在的文件夹应该什么都不做。 But that is not how Windows sees it.但这不是 Windows 的看法。

To avoid the error message, check first to see if the folder exists.为避免出现错误消息,请先检查文件夹是否存在。 Before the code that creates the folder, do在创建文件夹的代码之前,执行

if not os.path.exists("name of folder"):

I just encountered a slightly more subtle version of this, this might help someone else.我刚刚遇到了一个稍微微妙的版本,这可能对其他人有帮助。

I was creating a folder with:我正在创建一个文件夹:
os.makedirs(os.path.dirname(my_filename), exist_ok=True)

Which should create the folder, but not error if it already exists.哪个应该创建文件夹,但如果它已经存在则不会出错。 I'd run this numerous times with no problem.我会运行很多次没有问题。

Ran it again, and got an error:再跑一遍,报错:

FileExistsError: [WinError 183] Cannot create a file when that file already exists: '\my\folder\path'

Eventually worked out that I had kept the output folder from a previous run for comparison, by renaming it to something else.最终发现我保留了之前运行的输出文件夹以进行比较,方法是将其重命名为其他内容。
Which shouldn't have caused a problem, but I still had one of the files from that folder open in Notepad.这不应该引起问题,但我仍然在记事本中打开了该文件夹中的一个文件。 So since there was still an active reference to the folder the OS was throwing an error.因此,由于仍然有对该文件夹的活动引用,操作系统抛出了一个错误。

Closed the file in Notepad an re-ran, worked fine.在记事本中关闭文件重新运行,工作正常。

i had this error when i tried to create pathtree and then copy files there.当我尝试创建路径树然后在那里复制文件时出现此错误。 the actual problem was that - there was not a folder that already exists.实际的问题是 - 没有一个文件夹已经存在。

instead , there was a extension less file with same name.相反,有一个扩展名较少的同名文件。

so python cannot distinguish between file and folder here.所以python在这里无法区分文件和文件夹。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FileExistsError:[WinError 183] 当文件已存在时无法创建文件 - FileExistsError: [WinError 183] Cannot create a file when that file already exists 如何解决 FileExistsError: [WinError 183] 当文件已经存在时无法创建文件? - How to resolve 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] 当文件已经存在时,我在 watson 笔记本中收到文件未找到错误 - I am getting file not found error in watson notebook while the file already exists Python 创建目录错误 当文件已存在时无法创建文件 - Python create directory error Cannot create a file when that file already exists Google App Engine:“文件已经存在时无法创建” - Google App Engine: “Cannot create a file when that file already exists” 使用Python观察到该文件已存在时无法创建文件 - Cannot create a file when that file already exists error observed using Python 使用 Openai 的 whisper ai 时出现以下错误:FileNotFoundError: [WinError 2] The system cannot find the file specified - I am getting the following error while using Openai's whisper ai : FileNotFoundError: [WinError 2] The system cannot find the file specified
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM