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

相关文章
|
22天前
|
Python
【python】python跨文件使用全局变量
【python】python跨文件使用全局变量
|
1月前
|
监控 数据处理 索引
使用Python批量实现文件夹下所有Excel文件的第二张表合并
使用Python和pandas批量合并文件夹中所有Excel文件的第二张表,通过os库遍历文件,pandas的read_excel读取表,concat函数合并数据。主要步骤包括:1) 遍历获取Excel文件,2) 读取第二张表,3) 合并所有表格,最后将结果保存为新的Excel文件。注意文件路径、表格结构一致性及异常处理。可扩展为动态指定合并表、优化性能、日志记录等功能。适合数据处理初学者提升自动化处理技能。
22 1
|
1月前
|
存储 并行计算 Java
Python读取.nc文件的方法与技术详解
本文介绍了Python中读取.nc(NetCDF)文件的两种方法:使用netCDF4和xarray库。netCDF4库通过`Dataset`函数打开文件,`variables`属性获取变量,再通过字典键读取数据。xarray库利用`open_dataset`打开文件,直接通过变量名访问数据。文中还涉及性能优化,如分块读取、使用Dask进行并行计算以及仅加载所需变量。注意文件路径、变量命名和数据类型,读取后记得关闭文件(netCDF4需显式关闭)。随着科学数据的增长,掌握高效处理.nc文件的技能至关重要。
141 0
|
1月前
|
Python
python中文件和异常处理方法(二)
python中文件和异常处理方法(二)
13 0
|
1月前
|
Python
python中文件和异常处理方法(一)
python中文件和异常处理方法(一)
29 0
|
1月前
|
Python
python中文件和异常处理方法(三)
python中文件和异常处理方法(三)
20 0
|
6天前
|
存储 Python
用Python实现批量下载文件——代理ip排除万难
用Python实现批量下载文件——代理ip排除万难
|
6天前
|
JSON 关系型数据库 数据库
《Python 简易速速上手小册》第6章:Python 文件和数据持久化(2024 最新版)
《Python 简易速速上手小册》第6章:Python 文件和数据持久化(2024 最新版)
33 0
|
6天前
|
数据挖掘 索引 Python
Python 读写 Excel 文件
Python 读写 Excel 文件
12 0
|
7天前
|
数据安全/隐私保护 Python
Python文件与目录操作:面试中的高频考点
【4月更文挑战第15天】本文介绍了Python文件和目录操作的面试重点,包括文件的读写、目录遍历及权限管理。强调了文件关闭、异常处理、特殊文件判断以及权限位和权限字符串的理解。提供了代码示例,如读写文件、遍历目录和更改文件权限,帮助读者在面试中表现出色。掌握这些技能将对编程求职之路大有裨益。
17 0

热门文章

最新文章