Skip to main content
 首页 » 编程设计

python之无法使用 os.mkdir 创建目录,尽管它不存在

2025年05月04日15soundcode

我尝试使用 os.mkdir 创建一个目录,但它引发了 FileExistsError。

>>> import os 
>>> os.mkdir('test') 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
FileExistsError: [Errno 17] File exists: 'test' 

文件 test 确实存在,但它是一个普通文件而不是目录。

  0 -rw-r--r--  1 sparkandshine  staff       0 Jan 31 17:09 test 

在这种情况下,如何创建目录 test/?

请您参考如下方法:

操作系统不允许文件和目录同名。目录只是一种特殊类型的文件。

有关详细信息,请参阅以下内容:https://unix.stackexchange.com/questions/22447/why-cant-i-have-a-folder-and-a-file-with-the-same-name