mercredi 6 juillet 2016

Renaming a single file in python

I'm trying to rename an audio file but I keep getting OSError: [Errno 2] No such file or directory.

In my program, each user has a directory that holds the users files. I obtain the path for each user by doing the following:

current_user_path = os.path.join(current_app.config['UPLOAD_FOLDER'], user.username)

/Users/johnsmith/Documents/pythonprojects/project/files/john

Now I want to obtain the path for the existing file I want to rename:

current_file_path = os.path.join(current_user_path,form.currentTitle.data)

/Users/johnsmith/Documents/pythonprojects/project/files/john/File1

The path for the rename file will be:

new_file_path = os.path.join(current_user_path, form.newTitle.data)

/Users/johnsmith/Documents/pythonprojects/project/files/john/One

Now I'm just running the rename command:

os.rename(current_file_path, new_file_path)

Aucun commentaire:

Enregistrer un commentaire