I am stuck in an "error" during my folder creation.
First of all, this is the code I am using:
import os
import errno
import subprocess
try:
folder = os.makedirs(os.path.expanduser('~\\Desktop\\FOLDER'))
except OSError as e:
if e.errno != errno.EEXIST:
raise
print(os.path.isdir('~\\Desktop\\FOLDER'), '- FOLDER CREATED')
os.makedirs()
~
print()
True
False
~
print()
False
os.path.isdir('C:\\Users\\Bob\\Desktop\\FOLDER')
True
You are just missing the expanduser
method when calling isdir:
print(os.path.isdir(os.path.expanduser('~\\Desktop\\FOLDER')), '- FOLDER CREATED')
You don't really need the check at the end as well. Since if there is no exception, you can be sure that the creation is successful.
Here is a cleaner implementation:
try:
dirpath = os.path.expanduser('~\\Desktop\\FOLDER')
os.makedirs(dirpath)
print dirpath, "creation successful"
except OSError as e:
print dirpath, "creation failed"
if e.errno != errno.EEXIST:
raise