#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实现一个简单的脚本——文件类型转换
最新推荐文章于 2024-07-11 23:37:30 发布