天天看點

Python中的mkdir -p功能[重複]

本文翻譯自:mkdir -p functionality in Python [duplicate]

This question already has an answer here:

這個問題在這裡已有答案:
  • How can I safely create a nested directory? 如何安全地建立嵌套目錄? 25 answers 25個答案

Is there a way to get functionality similar to

mkdir -p

on the shell from within Python.

有沒有辦法在Python中從shell獲得類似于

mkdir -p

功能。

I am looking for a solution other than a system call.

我正在尋找除系統調用之外的解決方案。

I am sure the code is less than 20 lines, and I am wondering if someone has already written it?

我确信代碼少于20行,我想知道是否有人寫過它?

#1樓

參考:https://stackoom.com/question/2W9k/Python中的mkdir-p功能-重複

#2樓

In Python >=3.2, that's

在Python> = 3.2中,那就是
os.makedirs(path, exist_ok=True)
           

In earlier versions, use @tzot's answer .

在早期版本中,使用@ tzot的答案 。

#3樓

import os
import tempfile

path = tempfile.mktemp(dir=path)
os.makedirs(path)
os.rmdir(path)
           

#4樓

As mentioned in the other solutions, we want to be able to hit the file system once while mimicking the behaviour of

mkdir -p

.

正如其他解決方案中所提到的,我們希望能夠在模仿

mkdir -p

的行為的同時命中檔案系統一次。

I don't think that this is possible to do, but we should get as close as possible.

我不認為這是可能的,但我們應該盡可能接近。

Code first, explanation later:

代碼優先,稍後說明:
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
           

As the comments to @tzot's answer indicate there are problems with checking whether you can create a directory before you actually create it: you can't tell whether someone has changed the file system in the meantime.

由于對@ tzot的回答的注釋表明在實際建立目錄之前檢查是否可以建立目錄存在問題:在此期間您無法判斷是否有人更改了檔案系統。

That also fits in with Python's style of asking for forgiveness, not permission.

這也符合Python要求寬恕的風格,而不是許可。

So the first thing we should do is try to make the directory, then if it goes wrong, work out why.

是以我們要做的第一件事就是嘗試制作目錄,然後如果出錯,找出原因。

As Jacob Gabrielson points out, one of the cases we must look for is the case where a file already exists where we are trying to put the directory.

正如Jacob Gabrielson指出的那樣,我們必須尋找的案例之一就是我們試圖放置目錄的檔案已經存在。

With

mkdir -p

:

使用

mkdir -p

$ touch /tmp/foo
$ mkdir -p /tmp/foo
mkdir: cannot create directory '/tmp/foo': File exists
           

The analogous behaviour in Python would be to raise an exception.

Python中的類似行為是引發異常。

So we have to work out if this was the case.

是以,如果是這種情況,我們必須解決。

Unfortunately, we can't.

不幸的是,我們做不到。

We get the same error message back from makedirs whether a directory exists (good) or a file exists preventing the creation of the directory (bad).

我們從makedirs獲得相同的錯誤消息,無論目錄是否存在(正常)還是存在防止建立目錄(壞)的檔案。

The only way to work out what happened is to inspect the file system again to see if there is a directory there.

找出問題的唯一方法是再次檢查檔案系統,看看那裡是否有目錄。

If there is, then return silently, otherwise raise the exception.

如果有,則以靜默方式傳回,否則引發異常。

The only problem is that the file system may be in a different state now than when makedirs was called.

唯一的問題是檔案系統現在可能處于與調用makedirs時不同的狀态。

eg: a file existed causing makedirs to fail, but now a directory is in its place.

例如:存在導緻makedirs失敗的檔案,但現在一個目錄就在其位置。

That doesn't really matter that much, because the the function will only exit silently without raising an exception when at the time of the last file system call the directory existed.

這并不重要,因為在最後一個檔案系統調用目錄時,該函數隻會在不引發異常的情況下以靜默方式退出。

#5樓

Recently, I found this distutils.dir_util.mkpath :

最近,我發現了這個distutils.dir_util.mkpath :
In [17]: from distutils.dir_util import mkpath

In [18]: mkpath('./foo/bar')
Out[18]: ['foo', 'foo/bar']
           

#6樓

Function declaration;

功能聲明;
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
           

usage :

用法:
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))