Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
current directory issue
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get Current Directory From ShortCut. Oshadha 1 966 Jul-30-2022, 04:41 PM
Last Post: snippsat
  portable way to get current directory Skaperen 5 3,210 Jun-12-2019, 01:16 PM
Last Post: Skaperen
  CWD (Current working directory) pcsailor 5 4,671 Nov-11-2018, 10:14 AM
Last Post: Larz60+
  Importing module from outside of the current directory Annie 4 4,554 Mar-21-2017, 11:13 PM
Last Post: Annie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020