in Education by
How can I check the file going to be written exists in the directory, and if not , How can I create directory in Python. I already tried this: import os file_path = "/my/directory/filename.txt" directory = os.path.dirname(file_path) try: os.stat(directory) except: os.mkdir(directory) f = file(filename) But I missed os.path.exists ,and now I have: def ensure_dir(file_path): directory = os.path.dirname(file_path) if not os.path.exists(directory): os.makedirs(directory) Can I make this happen automatically, is there an "open" flag for the same? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
There are several methods to perform this task, I will discuss a few with you: You can try os.path.existsand for creation consider os.makedirsfor creation. import os if not os.path.exists(directory): os.makedirs(directory) If you face an error you can trap and examine it using embedded error code import os, errno try: os.makedirs(directory) except OSError as e: if e.errno != errno.EEXIST: raise 2. If you are using Python 3.5+ you can use pathlib, it creates directory without raising an exception if directory already exists. I you want to create the parents or don't need it, then skip the parents argument import pathlib pathlib.Path('/my/directory').mkdir(parents=1, exist_ok=1) To know more about this you can have a look at the following video tutorial:- Learn in detail about Python by enrolling in Intellipaat Python Course online and upskill.

Related questions

0 votes
    In Python What command can I use to find these: Location of file which I am executing, and Current directory ( ... script in Python) Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I have two fields a string field and a numeric field in a directory. The key of the dictionary as well as the ... based on the keys? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can I check if a key exists in a directory in Python, before updating the value of the key. I tried this ... better way to do this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    In the os module in Python, is there a way to find if a directory exists, something like: os.direxists(os ... in pseudocode True/False Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    How do I get a list of all files (and directories) in a given directory in Python? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    How can I safely create a nested directory?...
asked Jan 9, 2021 in Technology by JackTerrance
0 votes
    I am facing problems in _files which isn't defined if I execute the script with exec, ececfile and in _module_ ... help me with this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to find all the files in directory with .txt extension in Python? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    How can I delete a file or a folder in Python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how I can represent the equivalent of an Enum in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have an item and I want to count it's occurrence in a list, How can I do that in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How can I remove the last character of a string in a newline, In perl I used chomp function what's equivalent to that in Python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to put time delay in the script of Python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can I convert a str from uppercase, or half uppercase to lowercase? E.x. "Decimeter" to "decimeter" Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I get the number of elements of a list? Ex- items = [] items.append("one") items.append("two") items.append("three") Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
...