【问题标题】:Unable to create a directory with os.mkdir though it doesn't exist无法使用 os.mkdir 创建目录,尽管它不存在
【发布时间】:2017-06-17 05:12:30
【问题描述】:

我尝试使用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/

【问题讨论】:

  • 你不能在同一个目录中同时拥有同名的文件和目录。删除或重命名一个。
  • @Kevin,感谢您指出这一点。

标签: python mkdir python-os


【解决方案1】:

你不能有一个文件和一个目录命名相同的东西。 如果你有一个名为 test.txt 的文件或其他文件,它会起作用

~> touch test
~> mkdir test
mkdir: test: File exists

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 1970-01-01
      • 2016-01-23
      • 2018-10-11
      • 1970-01-01
      相关资源
      最近更新 更多