当前位置:网站首页>复制多个excel然后命名不同的名字
复制多个excel然后命名不同的名字
2022-06-26 12:37:00 【好了,ap】
代码如下:
import os,shutil
import sys
result=[]
with open('name.txt','r',encoding='utf-8') as f:#需要命名的名字的文本列表
for line in f:
result.append(list(line.strip('\n').strip().split(',')))
print(result)
def copy_files(): #定义函数名称
for i in result:
j=i[0]
for foldName, subfolders, filenames in os.walk(path): #用os.walk方法取得path路径下的文件夹路径,子文件夹名,所有文件名
for filename in filenames: #遍历列表下的所有文件名
if filename.endswith('.xlsx'): #当文件名以.txt后缀结尾时
new_name=filename.replace('.xlsx','%s.xlsx'%(j)) #为文件赋予新名字
shutil.copyfile(os.path.join(foldName,filename), os.path.join(path2,new_name)) #复制到新路径下,并重命名文件
print(filename,"copied as",new_name) #输出提示
if __name__ == '__main__':
path = r'D:\桌面文件\test2' #运行程序前,记得修改主文件夹路径!
path2 =r'D:\桌面文件\test3' #存放文件的新路径,不要放在原路径下,不然会多复制两份
copy_files() #调用定义的函数,注意名称与定义的函数名一致
边栏推荐
- Nodejs framework express and KOA
- Thinkphp5 query report: sqlstate[hy093]: invalid parameter number
- 程序员必备,一款让你提高工作效率N倍的神器uTools
- Wechat applet wx Request request encapsulation
- PHP get directory size
- NoSQL mongodb - 01 introduction to NoSQL and mongodb
- Word文档导出(使用固定模板)
- 简易数字电路交通灯设计
- ES6:迭代器
- 【Spark】.scala文件在IDEA中几种图标的解释
猜你喜欢
计组实践实验9——使用CMStudio设计基于分段模型机微程序指令(2)
简易数字电路交通灯设计
Stream流学习记录
Examples of how laravel uses with preload (eager to load) and nested query
Splunk iowait 报警的解决
TP5 thinkphp5 report serialization of'closure'is not allowed
"Pinduoduo and short video speed version", how can I roast!
软件测试测试常见分类有哪些?
Comparison of latest mobile phone processors in 2020 (with mobile phone CPU ladder diagram)
由错误<note: candidate expects 1 argument, 0 provided>引发的思考
随机推荐
PHP unit conversion
7-1 数的范围
国标GB28181协议EasyGBS视频平台TCP主动模式拉流异常情况修复
自定义封装下拉组件
Redis learning - 05 node JS client operation redis and pipeline pipeline
Adobe Acrobat阻止30款安全软件查看PDF文件 或存在安全风险
文件远程同步、备份神器rsync
Learning directory
NoSQL mongodb - 02 mongodb server installation, mongodb shell, basic concepts and visualization tools
nvm安装教程
Stream流学习记录
Xiaolong 888 was released, Xiaomi 11 was launched, and 14 manufacturers carried it in the first batch!
【Spark】.scala文件在IDEA中几种图标的解释
Vivado 错误代码 [DRC PDCN-2721] 解决
Xiaobai lazy special-win10-win11 one click installation version
Websocket and socket IO case practice
PHP laravel+gatewayworker completes im instant messaging and file transfer functions (Chapter 2: explanation of business logic)
sqlalchemy event listen Automatic generate CRUD excel
Nodejs get get/post request parameters
solo 博客系统的 rss 渲染失败