os.makedirs 在 macOS 上错误地捕获目录的无扩展名文件
os.makedirs wrongly catches extension-less files for directory on macOS
我正在尝试创建与文件同名的目录。有一个文件:readme
,它没有扩展名。它陷入 os.makedirs(directory)
声称该文件存在。
source = "Users/me/Desktop/parent"
dirpaths = ['readme', 'index', 'robots']
def func(directory,source=source):
directory = os.path.join(source,directory) #
os.makedirs(directory)
a = [func(directory) for directory in dirpaths]
>>> FileExistsError: [Errno 17] File exists: '/Users/me/Desktop/parent/readme'
我将带有# 的行更改为:
directory = os.path.join(source,directory+"/")
>>> NotADirectoryError: [Errno 20] Not a directory: '/Users/me/Desktop/parent/readme/'
存在无扩展名的同名文件时如何创建目录?
Python 3.7.3
事实证明,macOS 将目录和无扩展名文件视为相同。我尝试将名为 readme
的文件夹移动到 parent
,但它拒绝了。
os.path.isfile(source + "/" + "readme")
True
os.path.isfile(source + "/" + "readme/")
False
os.path.isdir(source + "/" + "readme/")
False
os.path.isdir(source + "/" + "readme")
False
如果这里有区别,是不是也可以用来创作?
目录只是特殊类型的文件。具体来说,它们只是 file mode bits
indicate that the file is a directory (see the bitmask stat.S_ISDIR
) 所在的文件。例如,作为八进制数的目录模式通常可能是 0o40755
和常规文件 0o100644
.
在大多数文件系统(包括 macOS)上,您可能没有目录和同一目录中同名的常规文件,也可能没有包含路径分隔符的文件名。这与对象存储形成对比,例如 s3,后者 而不是 实际上是一个文件系统。
亲眼看看,无论您是否指定尾部斜杠,都会采用相同的 inode:
>>> import os
>>> os.makedirs("./example")
>>> os.stat('./example/').st_ino == os.stat('./example').st_ino
True
我正在尝试创建与文件同名的目录。有一个文件:readme
,它没有扩展名。它陷入 os.makedirs(directory)
声称该文件存在。
source = "Users/me/Desktop/parent"
dirpaths = ['readme', 'index', 'robots']
def func(directory,source=source):
directory = os.path.join(source,directory) #
os.makedirs(directory)
a = [func(directory) for directory in dirpaths]
>>> FileExistsError: [Errno 17] File exists: '/Users/me/Desktop/parent/readme'
我将带有# 的行更改为:
directory = os.path.join(source,directory+"/")
>>> NotADirectoryError: [Errno 20] Not a directory: '/Users/me/Desktop/parent/readme/'
存在无扩展名的同名文件时如何创建目录?
Python 3.7.3
事实证明,macOS 将目录和无扩展名文件视为相同。我尝试将名为 readme
的文件夹移动到 parent
,但它拒绝了。
os.path.isfile(source + "/" + "readme")
True
os.path.isfile(source + "/" + "readme/")
False
os.path.isdir(source + "/" + "readme/")
False
os.path.isdir(source + "/" + "readme")
False
如果这里有区别,是不是也可以用来创作?
目录只是特殊类型的文件。具体来说,它们只是 file mode bits
indicate that the file is a directory (see the bitmask stat.S_ISDIR
) 所在的文件。例如,作为八进制数的目录模式通常可能是 0o40755
和常规文件 0o100644
.
在大多数文件系统(包括 macOS)上,您可能没有目录和同一目录中同名的常规文件,也可能没有包含路径分隔符的文件名。这与对象存储形成对比,例如 s3,后者 而不是 实际上是一个文件系统。
亲眼看看,无论您是否指定尾部斜杠,都会采用相同的 inode:
>>> import os
>>> os.makedirs("./example")
>>> os.stat('./example/').st_ino == os.stat('./example').st_ino
True