# 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. ascii codec can't decode byte 0xe8 in position 0:ordinal not in range(128)

    问题描述:一个在Django框架下使用Python编写的定时更新项目,在一个Linux系统下运行没有问题,在另外一台Linux系统下测试,报如下错误: ascii codec can't decode ...

  2. jQuery选择器大全(48个代码片段+21幅图演示)

    选择器是jQuery最基础的东西,本文中列举的选择器基本上囊括了所有的jQuery选择器,也许各位通过这篇文章能够加深对jQuery选择器 的理解,它们本身用法就非常简单,我更希望的是它能够提升个人编 ...

  3. ajax方法完整的事件流

  4. 关于SQLServer2005的学习笔记—异常捕获及处理

    转自:http://blog.csdn.net/baoqiangwang/article/details/5395874 SQLServer2005 提供了类似于 C# 和 C++ 语言中的异常处理的 ...

  5. eclipse中java文件报错:The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files

    问题:The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class fi ...

  6. SharePoint 2010 最佳实践学习总结------第2章 SharePoint Windows PowerShell指南

    第2章 SharePoint Windows PowerShell指南 SharePoint 2010是SharePoint系列产品中第一个开始支持Windows PowerShell的产品,在以前的 ...

  7. C++中的Overload、Override和Overwrite

    在C++语言中有一组基础的概念一直都容易混淆:Overload.Override和Overwrite分别表示什么意思?下面把这三个概念整理一下: 1. Overload(重载) 重载的概念最好理解,在 ...

  8. Eclipse设置UTF-8

    将Eclipse所有的设置都设置为UTF-8: 1) Window -> Preferences -> General -> Workspace -> Text file en ...

  9. UI-导航控制器的使用

    1.初始化导航栏控制器 2..设置导航栏的标题 3.跳到下一个页面 4.返回上一个页面 5.自定义返回页面 6.导航栏上的自定义返回按钮 7.两个导航栏显示隐藏的常用方法(当前页不显示)

  10. cmd命令运行php,php通过cmd运行文件

    一.cmd命令运行php 1.通过配置环境变量 >php "php文件" 如果要待参数 php -q "php文件" 参数 php获取参数 $a = $a ...