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

标签 python mkdir python-os

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

关于python - 无法使用 os.mkdir 创建目录,尽管它不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41963126/

相关文章:

python - 为什么我会得到 "TypeError: open() missing required argument ' 标志' (pos 2 )"or "TypeError : an integer is required (got type str)"when opening a file?

Python:从特定的 .env 文件中获取 env 变量的值

python - 如何使用 unicode 字符重命名/替换所有文件和关联文件夹的特定关键字?

python - 分布式处理 - AWS Sagemaker

python - 获取属性错误: __exit__ when working with Tensorflow and MNIST data

php - 如何让 CodeIgniter 创建新文件夹?

bash - 将图像移动到具有图像文件名的文件夹中

python - 日期时间:在 isoformat() 中强制微秒输出

python - 使用 python 从 .txt 文件检索数据?

bash - 基本问题: using a variable content as directory name in Linux