Search code examples
pythonfilerename

remove specific characters from the filename using Python


how to rename multiple files from a folder by deleting only the underline character?

Ex: 1_2_3.txt To 123.txt


Solution

  • Use Python's os package

    folder_path = "INSERT PATH TO YOUR FOLDER HERE"
    for filename in os.listdir(folder_path):
        if "_" in filename:
            new_filename = filename.replace("_", "")
            os.rename(os.path.join(folder_path, filename), os.path.join(folder_path, new_filename))