版权声明:士,不可以不弘毅,任重而道远 https://blog.csdn.net/superbeyone/article/details/82894171
感冒Python
1. 替换指定文件夹下文件名称
import os
folder = input("请输入文件夹名称\n")
src = input("请输入将要替换的名称\n")
dest = input("请输入替换后的名称\n")
file_names = os.listdir(folder)
for name in file_names:
old_file_name = folder+"/"+name
name = name.replace(src,dest)
new_file_name = folder+"/"+name
os.rename(old_file_name,new_file_name)
2. 获取指定文件夹的所有文件目录名称
import os
rootDir = input("请输入查找的目录位置\n")
folder = os.listdir(rootDir)
for i in range(0,len(folder)):
path = os.path.join(rootDir,folder[i]).replace(rootDir,"")
print(path.split("\\")[1])