thumbtackthief
thumbtackthief

Reputation: 6211

Can't save files to different directory

My script works to save the files to the same directory as the code. I want to save it somewhere else. I've verified the directory exists. I know this has been asked many times, but I feel like I'm doing what others have said and I'm still getting IOError: [Errno 2] No such file or directory:

    filepath = os.path.join('/Dropbox/music_files', new_filename)
    f = open(filepath, 'w+')
    f.write(content)
    f.close() 

Upvotes: 0

Views: 114

Answers (1)

laike9m
laike9m

Reputation: 19328

make sure that /Dropbox/music_files exists

If not, use os.makedirs('/Dropbox/music_files') to create the dir you want to save into

Also, context manager is always better.

import os

if not os.path.exists('/Dropbox/music_files'):
    os.makedirs('/Dropbox/music_files')

filepath = os.path.join('/Dropbox/music_files', new_filename)

with open(filepath, 'w+') as f:
    f.write(content)

Upvotes: 1

Related Questions