python 批量重命名文件后缀
# batch_file_rename.py
# Created: 6th August 2012 '''
This will batch rename a group of files in a given directory,
once you pass the current and new extensions
''' __author__ = 'Craig Richards'
__version__ = '1.0' import os
import sys def batch_rename(work_dir, old_ext, new_ext):
'''
This will batch rename a group of files in a given directory,
once you pass the current and new extensions
'''
# files = os.listdir(work_dir)
for filename in os.listdir(work_dir):
# Get the file extension
file_ext = os.path.splitext(filename)[1]
# Start of the logic to check the file extensions, if old_ext = file_ext
if old_ext == file_ext:
# Set newfile to be the filename, replaced with the new extension
newfile = filename.replace(old_ext, new_ext)
# Write the files
os.rename(
os.path.join(work_dir, filename),
os.path.join(work_dir, newfile)
) def main():
'''
This will be called if the script is directly invoked.
'''
# Set the variable work_dir with the first argument passed
work_dir = sys.argv[1]
# Set the variable old_ext with the second argument passed
old_ext = sys.argv[2]
# Set the variable new_ext with the third argument passed
new_ext = sys.argv[3]
batch_rename(work_dir, old_ext, new_ext) if __name__ == '__main__':
main()
python 批量重命名文件后缀的更多相关文章
- Python批量重命名文件
批量替换文件名中重复字符: # -*- coding: UTF-8 -*- import os path = raw_input("请输入文件夹路径:") oldname = ra ...
- 利用Python批量重命名文件夹下文件
#!/usr/bin/python # -*- coding: UTF-8 -*- # -*- coding:utf8 -*- import os from string import digits ...
- python 批量重命名文件
# -*- coding: utf-8 -*- import os import sys def rename(): path = input("路径(例如D:\\\\picture):&q ...
- python 批量重命名文件名字
import os print(os.path) img_name = os.listdir('./img') for index, temp_name in enumerate(img_name): ...
- Linux批量重命名文件
五种方法实现Linux批量重命名文件 Linux批量重命名文件是指对某些特定的文件统一进行重新命名,以改变原来一批文件的名称,这里介绍五种方法来实现. Linux批量重命名文件会涉及到改变一个字母.改 ...
- [svc]find+xargs/exec重命名文件后缀&文件操作工具小结
30天内的文件打包 find ./test_log -type f -mtime -30|xargs tar -cvf test_log.tar.gz awk运算-解决企业统计pv/ip问题 find ...
- cmd - 批量重命名文件
相信大家或多或少都遇到过类似的情况:从网上下载了好多图片(或者其他的文件),这些图片的名字往往都是些乱七八糟的字母数字的组合,我们想要一次性修改几十张上百张的图片的名字应该怎么办呢? 这里有两种方法, ...
- [转]【Windows小技巧】批量重命名文件
注:如果文件名包含空格,命令应写成ren "s0 (1).gif" s001.gif,简而言之,就是加上双引号!!!原因:系统将s0和(1).gif认为是两个参数,再加上后面的s0 ...
- Linux命令行bash批量重命名文件
本文介绍下,在linux下使用shell批量重命名文件的例子,有需要的朋友参考下吧. 在linux中,重命名文件名,需要用到mv命令.如果需要批量重命名名一批文件,就需要写bash脚本或命令行了. 例 ...
随机推荐
- Unity光照
广义地说,Unity有2种光源.1.动态光源 2.Backed Lighting 1.动态光源 就是实时计算的.只要摆光源就可以了 2.Backed Lighting 提前处理好光照贴图.贴在物体上 ...
- C#基础:Lambda表达式
从委托的角度来看,Lambda表达式与匿名方法没有区别.在[C#基础:匿名方法]一文中,我使用了匿名方法来调用List<T>的FindAll方法.从C# 3.0开始,在使用匿名方法的地方, ...
- PHP使用mail()函数发送邮件流程以及注意事项
1. 配置 php.ini sendmail_path = /usr/sbin/sendmail -f yourname@sth.com -t -i 2. 使用函数 mail($to, $subjec ...
- 一个不错的vim配置
set nocp set backspace=indent,eol,start set number set autoindent set nocompatible set bs=indent,eol ...
- vs2010编译lua-5.3.2
lua-5.3.2.tar.gz 解开在 D:\lua 1. 启动VS命令行 2. c:\program files\microsoft visual studio 10.0\vc\bin>cd ...
- Review Board——在线代码审查工具
代码审查(Code Review)不但可以提高质量,而且还是一个知识共享和指导的极好的手段.不幸的是,准备工作的辛苦和工具支持的缺乏让代码审查很容易被延至“稍后再议”.Review Board的目标便 ...
- 邮件发送工具类 SendMail.java
package com.util; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.Simp ...
- junit类找不到的问题解决
1. Class not found *******java.lang.ClassNotFoundException: ******* at java.net.URLClassLoader$1.ru ...
- JS使用百度地图API
尚未整理: <script type="text/javascript"> var map = new BMap.Map("dituContent" ...
- Python 数据排序和列表迭代和列表推导应用
1.In-place sorting 原地排序 data=[6,4,5,2,3,1] print ('before sort', data) data.sort() print ('after sor ...