Python Forum
current directory issue - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: current directory issue (/thread-14036.html)



current directory issue - saisankalpj - Nov-12-2018

i am having path variable as final_directory= os.getcwd()+"\\Logs\\"
and the code
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
This above code doesnt seem to create a Logs directory if not present.


RE: current directory issue - buran - Nov-12-2018

1. add print(os.getwd()) and check that CWD is indeed what you think it is
2. use final_directory = os.path.join(os.getcwd(), 'Logs')
3. it's better to use try/except clause, instead of above if
try:
    os.makedirs(final_directory)
except OSError:
    pass
given that CWD exists and you create just one sub-folder, you can also use just os.mkdir

try:
    os.mkdir(final_directory)
except FileExistsError:
    pass