用python实现一个简单的脚本——文件类型转换

#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文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值