i trying rename file in specific path, moves file @ same level folder containing before being renamed.
import os path_to_file = "/users/me/file.txt" #note in code, file name randomly generated path_before_file, file_name = os.path.split(path_to_file) # need file_name here, file name without rest of path: "file.txt" renamed_file = os.path.splitext(file_name)[0] + '_renamed' + os.path.splitext(file_name)[1] #this how renamed file want "file_renamed.txt" renamed_file_path = os.path.join(path_to_file, renamed_file) #"/users/me/file_renamed.txt" os.rename(path_to_file, renamed_file_path)
after renaming, "/users/me/file.txt" becomes "/users/file.txt"
renamed_file_path = os.path.join(path_to_file, renamed_file) #"/users/me/file_renamed.txt"
above line has bug. above line should below
renamed_file_path = os.path.join(path_before_file, renamed_file) #"/users/me/file_renamed.txt"
Comments
Post a Comment