Python 3.5:Not able to remove non alpha -numeric characters from file_name
Question: i have written a python script to rename all the files present in a folder by
removing all the numbers from the file name but this doesn't work . Note :Same
code works fine for python2.7

    
    
    import os
    
    def rename_files():
        #(1) get file names from a folder
        file_list = os.listdir(r"D:\prank")
        print(file_list)
        saved_path = os.getcwd()
        print("Current working Directory is " + saved_path)
        os.chdir(r"D:\prank")
        #(2) for each file ,rename filename
        for file_name in file_list:
            os.rename(file_name, file_name.translate(None,"0123456789"))
    rename_files()
    

Can anyone tell me how to make it work.Is the translate function which is not
working properly


Answer: The problem is with os.rename() portion of your code.

os.rename() requires you to give it a full path to the file/folder you want to
change it to, while you only gave it the file_name and not the full path.

You have to add the full path to the folders/files directory. so it should
look like this:

    
    
    def rename_files():
    
    # add the folder path
    
        folder_path = "D:\prank\\"
        file_list = os.listdir(r"D:\prank")
        print(file_list)
        saved_path = os.getcwd()
        print("Current working Directory is " + saved_path)
        os.chdir(r"D:\prank")
    
        # Concat the folder_path with file_name to create the full path.
    
        for file_name in file_list:
            full_path = folder_path + file_name
            print (full_path) # See the full path here.
            os.rename(full_path, full_path.translate(None, "0123456789"))
    

