# 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 批量重命名文件后缀的更多相关文章

  1. Python批量重命名文件

    批量替换文件名中重复字符: # -*- coding: UTF-8 -*- import os path = raw_input("请输入文件夹路径:") oldname = ra ...

  2. 利用Python批量重命名文件夹下文件

    #!/usr/bin/python # -*- coding: UTF-8 -*- # -*- coding:utf8 -*- import os from string import digits ...

  3. python 批量重命名文件

    # -*- coding: utf-8 -*- import os import sys def rename(): path = input("路径(例如D:\\\\picture):&q ...

  4. python 批量重命名文件名字

    import os print(os.path) img_name = os.listdir('./img') for index, temp_name in enumerate(img_name): ...

  5. Linux批量重命名文件

    五种方法实现Linux批量重命名文件 Linux批量重命名文件是指对某些特定的文件统一进行重新命名,以改变原来一批文件的名称,这里介绍五种方法来实现. Linux批量重命名文件会涉及到改变一个字母.改 ...

  6. [svc]find+xargs/exec重命名文件后缀&文件操作工具小结

    30天内的文件打包 find ./test_log -type f -mtime -30|xargs tar -cvf test_log.tar.gz awk运算-解决企业统计pv/ip问题 find ...

  7. cmd - 批量重命名文件

    相信大家或多或少都遇到过类似的情况:从网上下载了好多图片(或者其他的文件),这些图片的名字往往都是些乱七八糟的字母数字的组合,我们想要一次性修改几十张上百张的图片的名字应该怎么办呢? 这里有两种方法, ...

  8. [转]【Windows小技巧】批量重命名文件

    注:如果文件名包含空格,命令应写成ren "s0 (1).gif" s001.gif,简而言之,就是加上双引号!!!原因:系统将s0和(1).gif认为是两个参数,再加上后面的s0 ...

  9. Linux命令行bash批量重命名文件

    本文介绍下,在linux下使用shell批量重命名文件的例子,有需要的朋友参考下吧. 在linux中,重命名文件名,需要用到mv命令.如果需要批量重命名名一批文件,就需要写bash脚本或命令行了. 例 ...

随机推荐

  1. SET ROWCOUNT,SET NOCOUNT

    SET ROWCOUNT (Transact-SQL) 在停止特定查询之前要处理的行数(整数). 重要提示 在 SQL Server 的将来版本中,使用 SET ROWCOUNT 将不会影响 DELE ...

  2. webpack es6支持配置

    Install Babel and the presets: npm install --save-dev babel-core babel-preset-es2015 Install babel-l ...

  3. Deferred和Promise之间有什么区别呢?

    一个promise就是一个由异步函数返回的对象. deferred对象就是jQuery的回调函数解决方案. 总结 jQuery 的ajax 就是返回一个promise 对象,里面含有done(), f ...

  4. Hive(一):架构及知识体系

    Hive是一个基于Hadoop的数据仓库,最初由Facebook提供,使用HQL作为查询接口.HDFS作为存储底层.mapReduce作为执行层,设计目的是让SQL技能良好,但Java技能较弱的分析师 ...

  5. maven本地仓库的配置以及如何修改默认.m2仓库位置

    本地仓库是远程仓库的一个缓冲和子集,当你构建Maven项目的时候,首先会从本地仓库查找资源,如果没有,那么Maven会从远程仓库下载到你本地仓库.这样在你下次使用的时候就不需要从远程下载了.如果你所需 ...

  6. jfinal配置rails的数据表

    鉴于rails的部署太可怕,所以有了使用rails的建表工具和migration,用jfinal来开发的想法,在此贴一下需要注意的地方 maven配置 <dependency> <g ...

  7. MSSQL update 多表关联更新

    update tMeter set 字段= t.源自段 from ( select * from 源表信息 ) t where 关联条件 实际demo: UPDATE dbo.WX_TWODIMENC ...

  8. 36. Valid Sudoku

    ============= Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku b ...

  9. mongodb3.03开启认证

    原文地址:http://21jhf.iteye.com/blog/2216103 下载了最新mongodb3.03版本,当使用--auth 参数命令行开启mongodb用户认证时遇到很多问题,现总结如 ...

  10. nginx 负载均衡-- 常用nginx配置

    中文官方网站http://wiki.nginx.org/Chshttp://www.howtocn.org/ --------------------------------------------- ...