简体   繁体   中英

Python File Check and Creation

I am trying to check or create a directory and I am getting this error:

/usr/bin/python3.6 /home/user/Development/americas.py Traceback (most recent call last): File "/home/dquezada/Development/americas.py", line 23, in os.mkdir(path) FileNotFoundError: [Errno 2] No such file or directory: '/home/dquezada/Development/data/maps/americas/'

Process finished with exit code 1

Below is my code:

path = '/home/user/Development/data/maps/americas/'

# Check if path exists, if it does not it creates it
if not os.path.exists(path):
    os.mkdir(path)

wm.render_to_file(path + timeStamp + '_americas.svg')

I believe os.path.isdir(path) is what you are looking for. This will return True if it is a directory, or False if it isn't. os.path.exists(path) is not intended for what you are using it for.

path = '/home/user/Development/data/maps/americas/'

# Check if path exists, if it does not it creates it
if not os.path.isdir(path):
    os.mkdir(path)

wm.render_to_file(path + timeStamp + '_americas.svg')

That should work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM