是否有一种方法可以从Python内部获得类似于shell上的mkdir -p的功能。我正在寻找一个解决方案,而不是系统调用。我确信代码少于20行,我想知道是否有人已经写了它?
当前回答
使用python3标准库中的Pathlib:
Path(mypath).mkdir(parents=True, exist_ok=True)
If parents is true, any missing parents of this path are created as needed; they are created with the default permissions without taking mode into account (mimicking the POSIX mkdir -p command). If exist_ok is false (the default), an FileExistsError is raised if the target directory already exists. If exist_ok is true, FileExistsError exceptions will be ignored (same behavior as the POSIX mkdir -p command), but only if the last path component is not an existing non-directory file. Changed in version 3.5: The exist_ok parameter was added.
其他回答
正如在其他解决方案中提到的,我们希望能够在模仿mkdir -p的行为时访问文件系统一次。我认为这是不可能的,但我们应该尽可能接近。
先写代码,再解释:
import os
import errno
def mkdir_p(path):
""" 'mkdir -p' in Python """
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
正如@tzot回答的注释所指出的那样,在实际创建目录之前检查是否可以创建目录存在问题:您无法判断是否有人同时更改了文件系统。这也符合Python请求原谅的风格,而不是请求许可。
所以我们要做的第一件事就是建立目录,如果出错,找出原因。
正如Jacob Gabrielson指出的那样,我们必须寻找的一种情况是,在我们试图放置目录的地方已经存在一个文件。
使用mkdir -p:
$ touch /tmp/foo
$ mkdir -p /tmp/foo
mkdir: cannot create directory '/tmp/foo': File exists
Python中的类似行为是引发异常。
所以我们要算出是不是这样。不幸的是,我们不能。我们从makedirs得到相同的错误消息,无论目录是否存在(好)或文件是否存在,都将阻止目录的创建(坏)。
弄清楚发生了什么事的唯一方法是再次检查文件系统,看看那里是否有一个目录。如果存在,则静默返回,否则引发异常。
唯一的问题是,文件系统现在的状态可能与调用makedirs时的状态不同。例如:一个文件存在导致makedirs失败,但是现在在它的位置上有一个目录。这其实没有太大关系,因为该函数只会在最后一次文件系统调用目录时静默退出,而不会引发异常。
import os
import tempfile
path = tempfile.mktemp(dir=path)
os.makedirs(path)
os.rmdir(path)
在Python中>=3.2,这是
os.makedirs(path, exist_ok=True)
在早期版本中,使用@tzot的答案。
函数声明;
import os
def mkdir_p(filename):
try:
folder=os.path.dirname(filename)
if not os.path.exists(folder):
os.makedirs(folder)
return True
except:
return False
用法:
filename = "./download/80c16ee665c8/upload/backup/mysql/2014-12-22/adclient_sql_2014-12-22-13-38.sql.gz"
if (mkdir_p(filename):
print "Created dir :%s" % (os.path.dirname(filename))
使用python3标准库中的Pathlib:
Path(mypath).mkdir(parents=True, exist_ok=True)
If parents is true, any missing parents of this path are created as needed; they are created with the default permissions without taking mode into account (mimicking the POSIX mkdir -p command). If exist_ok is false (the default), an FileExistsError is raised if the target directory already exists. If exist_ok is true, FileExistsError exceptions will be ignored (same behavior as the POSIX mkdir -p command), but only if the last path component is not an existing non-directory file. Changed in version 3.5: The exist_ok parameter was added.