我想把a.txt改为b.kml。


当前回答

如果你正在使用Windows,你想重命名文件夹中的1000s文件,然后: 您可以使用下面的代码。(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!!")

其他回答

import shutil

shutil.move('a.txt', 'b.kml')

这将用于重命名或移动文件。

使用os.rename。但是你必须把两个文件的完整路径传递给函数。如果我的桌面上有一个。txt文件那么我就会这样做,而且我也必须给出一个完整的重命名文件。

os.rename('C:\\Users\\Desktop\\a.txt', 'C:\\Users\\Desktop\\b.kml')

使用os.rename:

import os

os.rename('a.txt', 'b.kml')

用法:

os.rename('from.extension.whatever','to.another.extension')
import os

# Set the path
path = 'a\\b\\c'  
# save current working directory
saved_cwd = os.getcwd()
# change your cwd to the directory which contains files
os.chdir(path)
os.rename('a.txt', 'b.klm')
# moving back to the directory you were in 
os.chdir(saved_cwd)
import os
import re
from pathlib import Path

for f in os.listdir(training_data_dir2):
  for file in os.listdir( training_data_dir2 + '/' + f):
    oldfile= Path(training_data_dir2 + '/' + f + '/' + file)
    newfile = Path(training_data_dir2 + '/' + f + '/' + file[49:])
    p=oldfile
    p.rename(newfile)