How to rename a file using Python

后端 未结 12 1698
借酒劲吻你
借酒劲吻你 2020-11-22 13:47

I want to change a.txt to b.kml.

12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 14:29

    If you are Using Windows and you want to rename your 1000s of files in a folder then: You can use the below code. (Python3)

    import os
    
    path = os.chdir(input("Enter the path of the Your Image Folder :  ")) #Here put the path of your folder where your images are stored
    
    image_name = input("Enter your Image name : ") #Here, enter the name you want your images to have
    
    i = 0
    
    for file in os.listdir(path):
    
        new_file_name = image_name+"_" + str(i) + ".jpg" #here you can change the extention of your renmamed file.
        os.rename(file,new_file_name)
    
        i = i + 1
    
    input("Renamed all Images!!")
    

提交回复
热议问题