0

For some silly reason, the below works on creating the non-existing directory stored in output_result_dir, but not the non_existing directory stored in output_log_dir. The latter results in a FileNotFound Error with description "[WinError 3] The system cannot find the path specified: 'runpackage1\calculated\logs'"

log_dir = os.path.join(output_dir, 'logs')
output_config_dir = os.path.join(output_dir, 'config')
output_result_dir = os.path.join(output_dir, 'results')
for directory in [output_result_dir, log_dir, output_config_dir]:
    if not os.path.exists(directory):
        os.makedirs(directory)

Am I missing something really stupid here? I also tried inserting an os.path.abspath around the path to get from relative to full paths, but that didn't help either.

Edit: changed directory from path as some people rightfully pointed out. I actually copied code from two separate functions into one self-contained block here, hence introducing the error. It's not the issue that caused my problem.

4
  • Use directory not path Commented Dec 27, 2018 at 9:44
  • by creating your minimal reproducible example you fixed your code --- can't confirm it Commented Dec 27, 2018 at 9:52
  • No, I didn't fix my code. It's still giving the same error. Commented Dec 27, 2018 at 9:54
  • Turns out the error was produced because (as part of a sync conflict) windows had a lock on a directory that no longer existed on my local PC. So the code was fine from the beginning (apart from me having pulled it together from two different functions and not having renamed a variable), this was just somewhat obscure windows behaviour. Should I: - close the question - answer my own (a bit off-topic for future searches based on the title, but it might help people) - mark one of the given questions as answers (which miss the underlying issue) Commented Dec 27, 2018 at 15:43

2 Answers 2

1

Not able to confirm your bug (https://pyfiddle.io - 3.6):

import os 

for root,dirs,files in os.walk("./"):
    print (root,dirs)
print("")

output_dir = "./temp"
log_dir = os.path.join(output_dir, 'logs')
output_config_dir = os.path.join(output_dir, 'config')
output_result_dir = os.path.join(output_dir, 'results')
for directory in [output_result_dir, log_dir, output_config_dir]:
    os.makedirs(directory)

for root,dirs,files in os.walk("./"):
    print (root,dirs)

Output:

('./', [])        # before

('./', ['temp'])  # after
('./temp', ['results', 'logs', 'config'])
('./temp/results', [])
('./temp/logs', [])

Creating the mcve fixed your problem.


log_dir = os.path.join(output_dir, 'logs')
output_config_dir = os.path.join(output_dir, 'config')
output_result_dir = os.path.join(output_dir, 'results')
for directory in [output_result_dir, log_dir, output_config_dir]:   # using directory
    if not os.path.exists(path):                                    # using path
        os.makedirs(path)                                           # using path

You also do not need the guard if not os.path.exists(path): - it is not needed because if the directory already exists - using os.makedirs(path) won't change that provided you give exists_ok=True.

Fix:

log_dir = os.path.join(output_dir, 'logs')
output_config_dir = os.path.join(output_dir, 'config')
output_result_dir = os.path.join(output_dir, 'results')
for directory in [output_result_dir, log_dir, output_config_dir]:   # directory
    os.makedirs(directory, exists_ok=True)                          # directory
Sign up to request clarification or add additional context in comments.

1 Comment

With regard to makedirs, The python 3 syntax is slightly different to when working with existing directories: os.makedirs(name, mode=0o777, exist_ok=False) docs.python.org/3/library/os.html#os.makedirs
0

The given code has no errors, you fixed it.

output_dir = '.'
log_dir = os.path.join(output_dir, 'logs')
output_config_dir = os.path.join(output_dir, 'config')
output_result_dir = os.path.join(output_dir, 'results')
for directory in [output_result_dir, log_dir, output_config_dir]:
    if not os.path.exists(directory):
        os.makedirs(directory)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.