import os
dirname = 'create/me'
try:os.makedirs(dirname)except OSError:if os.path.exists(dirname):# We are nearly safepasselse:# There was an error on creation, so make sure we know about itraise
mkdtemp(suffix='', prefix='tmp', dir=None)User-callable function to create and return a unique temporarydirectory. The return value is the pathname of the directory.
The directory is readable, writable, and searchable only by thecreating user.
Caller is responsible for deleting the directory when done with it.
import errnotry:with open(filepath) as my_file:do_stuff(my_file)except IOError as error:if error.errno == errno.ENOENT:print 'ignoring error because directory or file is not there'else:raise
import osimport errnoif not os.path.exists(directory):try:os.makedirs(directory)except OSError as error:if error.errno != errno.EEXIST:raisewith open(filepath, 'w') as my_file:do_stuff(my_file)
from pathlib import Pathpath = Path("/my/directory/filename.txt")try:if not path.parent.exists():path.parent.mkdir(parents=True)except OSError:# handle error; you can also catch specific errors like# FileExistsError and so on.
import osimport errno
def make_sure_path_exists(path):try:os.makedirs(path)except OSError as exception:if exception.errno != errno.EEXIST or not os.path.isdir(path):raise
# Create a directory and any missing ancestor directories.# If the directory already exists, do nothing.
from distutils.dir_util import mkpathmkpath("test")
└── output/ ## dir├── corpus ## file├── corpus2/ ## dir└── subdir/ ## dir
以下是我的实验/笔记,它提供了澄清:
# ----------------------------------------------------------------------------# [1] https://stackoverflow.com/questions/273192/how-can-i-create-a-directory-if-it-does-not-exist
import pathlib
""" Notes:1. Include a trailing slash at the end of the directory path("Method 1," below).2. If a subdirectory in your intended path matches an existing filewith same name, you will get the following error:"NotADirectoryError: [Errno 20] Not a directory:" ..."""# Uncomment and try each of these "out_dir" paths, singly:
# ----------------------------------------------------------------------------# METHOD 1:# Re-running does not overwrite existing directories and files; no errors.
# out_dir = 'output/corpus3' ## no error but no dir created (missing tailing /)# out_dir = 'output/corpus3/' ## works# out_dir = 'output/corpus3/doc1' ## no error but no dir created (missing tailing /)# out_dir = 'output/corpus3/doc1/' ## works# out_dir = 'output/corpus3/doc1/doc.txt' ## no error but no file created (os.makedirs creates dir, not files! ;-)# out_dir = 'output/corpus2/tfidf/' ## fails with "Errno 20" (existing file named "corpus2")# out_dir = 'output/corpus3/tfidf/' ## works# out_dir = 'output/corpus3/a/b/c/d/' ## works
# [2] https://docs.python.org/3/library/os.html#os.makedirs
# Uncomment these to run "Method 1":
#directory = os.path.dirname(out_dir)#os.makedirs(directory, mode=0o777, exist_ok=True)
# ----------------------------------------------------------------------------# METHOD 2:# Re-running does not overwrite existing directories and files; no errors.
# out_dir = 'output/corpus3' ## works# out_dir = 'output/corpus3/' ## works# out_dir = 'output/corpus3/doc1' ## works# out_dir = 'output/corpus3/doc1/' ## works# out_dir = 'output/corpus3/doc1/doc.txt' ## no error but creates a .../doc.txt./ dir# out_dir = 'output/corpus2/tfidf/' ## fails with "Errno 20" (existing file named "corpus2")# out_dir = 'output/corpus3/tfidf/' ## works# out_dir = 'output/corpus3/a/b/c/d/' ## works
# Uncomment these to run "Method 2":
#import os, errno#try:# os.makedirs(out_dir)#except OSError as e:# if e.errno != errno.EEXIST:# raise# ----------------------------------------------------------------------------
import os
def create_dir(directory):if not os.path.exists(directory):print('Creating Directory '+directory)os.makedirs(directory)
create_dir('Project directory')