#coding:utf-8
import os,sys
#用python实现一个简单的脚本,输入目录,等待转换的文件类型 转换成什么类型
MycontentsPath= sys.argv[1]
Waitfile = sys.argv[2]
Wantedfile = sys.argv[3]
print(MycontentsPath,Waitfile,Wantedfile)
files = os.listdir(MycontentsPath)
try:
for filename in files:
portion = os.path.splitext(filename)
if portion[1] == Waitfile:
newname = portion[0] + Wantedfile
os.chdir(MycontentsPath) #如果不更改一下路径,没办法改名字的。
os.rename(filename,newname)
print("the program is finished!")
except Exception as e:
print(e)
print("your path is error ,try again!!!")
#使用方法:直接在黑窗口执行。例如 python 改文件名.py C:\Users\Administrator\PycharmProjects\爬虫面试 .txt .py
#意思是在C:\Users\Administrator\PycharmProjects\爬虫面试下把所有的.txt文件转换成.py文件。
用python实现一个简单的脚本——文件类型转换
最新推荐文章于 2023-11-28 09:15:34 发布
本文介绍了一个使用Python编写的简单脚本,该脚本能够批量将指定目录下的某类文件扩展名转换为另一种扩展名。只需提供目标目录、当前文件类型及目标文件类型即可执行转换。
9万+

被折叠的 条评论
为什么被折叠?



