• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python实现批量更换指定目录下文件扩展名的方法

Python实现批量更换指定目录下文件扩展名的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python 文件扩展名,文件扩展名指定格式,python文件的扩展名为,文件扩展名,怎么显示文件的扩展名等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了Python实现批量更换指定目录下文件扩展名的方法。分享给大家供大家参考,具体如下:

#encoding=utf-8
#author: walker
#date: 2013-12-06
#function: 深度遍历指定目录,更换指定扩展名
import os
import os.path
#读入指定目录并转换为绝对路径
rootdir = raw_input('root dir:\n')
rootdir = os.path.abspath(rootdir)
print('absolute path:\n' + rootdir)
#读入原扩展名并标准化
old_ext = raw_input('old extension:\n')
old_ext = old_ext.strip()
if old_ext[0] != '.':
  old_ext = '.' + old_ext
#读入新扩展名并标准化
new_ext = raw_input('new extension:\n')
new_ext = new_ext.strip()
if new_ext[0] != '.':
  new_ext = '.' + new_ext
for parent, dirnames, filenames in os.walk(rootdir):
  for filename in filenames:
    pathfile = os.path.join(parent, filename)
    if pathfile.endswith(old_ext):
      new_pathfile = os.path.splitext(pathfile)[0] + new_ext
      print('=======================================================')
      print(pathfile)
      print('-------------------------------------------------------')
      print(new_pathfile)
      print('=======================================================')
      os.rename(pathfile, new_pathfile)

</div>

PS:上述功能一个shell命令也可以实现

#将后缀.ini换成.txt
#路径名可以是相对路径或绝对路径
find 路径名 | rename 's/\.ini$/\.txt/'

</div>

注意,上面的rename命令是perl版的rename命令。

PS2:scandir的兼容代码。

# Use the built-in version of scandir/walk if possible, otherwise
# use the scandir module version
try:
  from os import scandir, walk  #python3.5+
except ImportError:
  from scandir import scandir, walk #python3.4-

</div>

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python URL操作技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python实现批量更换指定目录下文件扩展名的方法
  • python获取文件扩展名的方法
  • python根据给定文件返回文件名和扩展名的方法

相关文章

  • python静态方法实例
  • 详解Python的Django框架中Manager方法的使用
  • Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法
  • 讲解Python中的递归函数
  • Python中exit、return、sys.exit()等使用实例和区别
  • Python环境下搭建属于自己的pip源的教程
  • Python中字符串对齐方法介绍
  • Python实现 多进程导入CSV数据到 MySQL
  • 详解Python中的条件判断语句
  • python中管道用法入门实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python缩进区别分析
    • python统计文本字符串里单词出现频率的方法
    • Python编程中的文件操作攻略
    • Python的subprocess模块总结
    • Python制作Windows系统服务
    • python动态监控日志内容的示例
    • python根据给定文件返回文件名和扩展名的方法
    • Python基于pillow判断图片完整性的方法
    • win7上python2.7连接mysql数据库的方法
    • Python处理JSON时的值报错及编码报错的两则解决实录

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有