os.chdir returns No such file or directory: 'None'

1.2k Views Asked by At

How come that these lines of code creates a folder, but returns an error output?

key_ = "Test"
new_folder = os.makedirs(str(key_)+str(datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M')))
os.chdir(str(new_folder))

Folder

The error I'm getting is

line 457, in download_url
os.chdir(str(new_folder))
FileNotFoundError: [Errno 2] No such file or directory: 'None'
2

There are 2 best solutions below

0
AnxiousDino On BEST ANSWER

This seems to be the solution:

file_path = os.path.realpath(__file__)
output_files = file_path.replace('billede_indsamling.py',str(output_dir))
os.chdir(output_files)
new_folder = str(key_)+str(datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d'))
#os.mkdir(new_folder) 
path = os.path.join(output_files, new_folder)

if not os.path.exists(path):
    os.makedirs(path)
    print("Directory '%s' created successfully" %new_folder)
elif os.path.exists(path):
    os.chdir(path)
2
Barmar On

os.makedirs() doesn't return the name of the directory it created. Assign the directory name to a variable first, and use that in both function calls.

key_ = "Test"
new_folder = str(key_)+str(datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M'))
os.makedirs(new_folder, exist_ok=True)
os.chdir(new_folder)