1 Answer

0 votes
by
use pathlib.Path.mkdir: from pathlib import Path Path("/my/directory").mkdir(parents=True, exist_ok=True) For older versions of Python, I see two answers with good qualities, each with a small flaw, so I will give my take on it: Try os.path.exists, and consider os.makedirs for the creation. import os if not os.path.exists(directory): os.makedirs(directory) As noted in comments and elsewhere, there's a race condition – if the directory is created between the os.path.exists and the os.makedirs calls, the os.makedirs will fail with an OSError. Unfortunately, blanket-catching OSError and continuing is not foolproof, as it will ignore a failure to create the directory due to other factors, such as insufficient permissions, full disk, etc. One option would be to trap the OSError and examine the embedded error code import os, errno try: os.makedirs(directory) except OSError as e: if e.errno != errno.EEXIST: raise Alternatively, there could be a second os.path.exists, but suppose another created the directory after the first check, then removed it before the second one – we could still be fooled. Depending on the application, the danger of concurrent operations may be more or less than the danger posed by other factors such as file permissions. The developer would have to know more about the particular application being developed and its expected environment before choosing an implementation. Modern versions of Python improve this code quite a bit, both by exposing FileExistsError (in 3.3+)... try: os.makedirs("path/to/directory") except FileExistsError: # directory already exists pass

Related questions

0 votes
    How can I check the file going to be written exists in the directory, and if not , How can I create directory ... " flag for the same? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I need to make a table with data with nested subtasks. I need to make this table recursive. There ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I am actually learning smart contract programming on ethereum and I work with truffle. Right now I am ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    Under Version control, how you will create a new directory in SVN?...
asked Feb 21, 2021 in Technology by JackTerrance
0 votes
    How to create table in Data Directory in SAP with steps?...
asked May 31, 2021 in Education by JackTerrance
0 votes
    Create a nested list as follows using both ordered and unordered list tags: 1. Operating System o Disk operating ... Unix o Linux Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    I want to upgrade my Amazon EC2 micro (t1.micro) instance to large. Please help. Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    Which module will you use to create a directory? (i)file (ii)fetch (iii)copy (iv)template...
asked Jan 24, 2023 in Technology by JackTerrance
0 votes
    Name the command to create a new directory under version control?...
asked Feb 21, 2021 in Technology by JackTerrance
+1 vote
    Is it possible to create a custom domain name, or use your organisation's domain name such as eduforum.in, in Azure Active Directory?...
asked Oct 20, 2020 in Technology by JackTerrance
0 votes
    Which method is used to create a directory with fileattributes? (a) Path.create() (b) Path.createDirectory() ... Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which method is used to create a directory with fileattributes? (a) Path.create() (b) Path.createDirectory( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    Why is it necessary to safely store the pathogens of a disease against which vaccines are to be ... ,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    How do I list all files of a directory?...
asked Jan 9, 2021 in Technology by JackTerrance
...