Python批量更换指定目录文件的扩展名

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#encoding: utf-8
#author: walker
#date: 2013-12-06
#summary: 深度遍历指定目录,更换指定扩展名
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)


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

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

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


PS2:scandir的兼容代码。

1
2
3
4
5
6
# 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-


*** walker ***

本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1337457如需转载请自行联系原作者


RQSLT

相关文章
|
3天前
|
Shell Python
Python Stock guess_indicators_daily_job.py文件的调整
Python Stock guess_indicators_daily_job.py文件的调整
12 1
|
3天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
11 1
|
1天前
|
Python
在Python中,利用`os模块`的`path.exists()`函数可判断文件是否存
在Python中,利用`os模块`的`path.exists()`函数可判断文件是否存在,该函数对路径进行检查,存在则返回True,不存在则返回False。示例代码展示了如何检查'example.txt'文件是否存在并相应打印消息。此外,`os.path.isfile()`用于确认路径是否为文件,仅当是文件时返回True,否则返回False,同样配以示例说明其用法。
8 2
|
2天前
|
Python
【Python进阶(五)】——模块搜索及工作目录
【Python进阶(五)】——模块搜索及工作目录
|
4天前
|
存储 Linux Shell
python移除/删除非空文件夹/目录的最有效方法是什么?
python移除/删除非空文件夹/目录的最有效方法是什么?
8 0
|
4天前
|
数据采集 NoSQL 中间件
python-scrapy框架(四)settings.py文件的用法详解实例
python-scrapy框架(四)settings.py文件的用法详解实例
9 0
|
4天前
|
存储 数据采集 数据库
python-scrapy框架(三)Pipeline文件的用法讲解
python-scrapy框架(三)Pipeline文件的用法讲解
7 0
|
6天前
|
缓存 数据处理 Python
python读取文件到缓存
python读取文件到缓存
12 1
|
7天前
|
存储 数据挖掘 Python
Python技术分享:实现选择文件或目录路径的方法
Python技术分享:实现选择文件或目录路径的方法
17 2
|
7天前
|
前端开发 JavaScript Python
使用Python读取本地行情csv文件,做出web网页画出K线图实现案例
【5月更文挑战第4天】使用Python绘制K线图的步骤:1) 安装pandas, matplotlib和Flask;2) 用pandas读取CSV文件并处理数据;3) 创建Flask应用,渲染包含K线图数据的HTML;4) 编写HTML,使用ECharts库绘制K线图。
26 0