Swapping One File Extension for Another Throughout a Directory Tree
2019-08-01 21:24
1806 查看
原文链接:https://www.geek-share.com/detail/2446201400.html
1 >>> import os
2 >>> def swapextensions(dir, before, after):
3 if before[:1] != '.':
4 before = '.'+before
5 length = -len(before)
6 if after[:1] != '.':
7 after = '.'+after
8 for path, dirs, files in os.walk(dir):
9 for oldfile in files:
10 if oldfile[length:] == before:
11 oldfile = os.path.join(path, oldfile)
12 newfile = oldfile[:length] + after
13 os.rename(oldfile, newfile)
1 >>> for path, dirs, files in os.walk('c:\\Test'):
2 for f in files:
3 print f
4
5
6 3545.csv
7 abc.txt
8 e.xml
1 >>> swapextensions('c:\\Test', 'txt', 'csv')
1 >>> for path, dirs, files in os.walk('c:\\Test'):
2 for f in files:
3 print f
4
5
6 3545.csv
7 abc.csv
8 e.xml
Problem
You need to rename files throughout a subtree of directories, specifically changing the names of all files with a given extension so that they have a different extension instead.
Solution
Operating on all files of a whole subtree of directories is easy enough with the os.walk function from Python's standard library:
SwapExtensions1 >>> import os
2 >>> def swapextensions(dir, before, after):
3 if before[:1] != '.':
4 before = '.'+before
5 length = -len(before)
6 if after[:1] != '.':
7 after = '.'+after
8 for path, dirs, files in os.walk(dir):
9 for oldfile in files:
10 if oldfile[length:] == before:
11 oldfile = os.path.join(path, oldfile)
12 newfile = oldfile[:length] + after
13 os.rename(oldfile, newfile)
Usage
Before swap:
Before swap1 >>> for path, dirs, files in os.walk('c:\\Test'):
2 for f in files:
3 print f
4
5
6 3545.csv
7 abc.txt
8 e.xml
Swap:
Swap1 >>> swapextensions('c:\\Test', 'txt', 'csv')
After swap:
After swap1 >>> for path, dirs, files in os.walk('c:\\Test'):
2 for f in files:
3 print f
4
5
6 3545.csv
7 abc.csv
8 e.xml
转载于:https://www.cnblogs.com/zhtf2014/archive/2009/02/21/1395440.html
相关文章推荐
- Swapping One File Extension for Another Throughout a Directory Tree
- windows api一日一练(dbghelp sdk)-002-MakeSureDirectoryPathExists,SearchTreeForFile
- ogr2ogr: Export Well Known Text (WKT) for one feature to a CSV file
- A file with the correct name was found in this directory, but it is not the correct one.
- Cocos2d-x 3.0 beta 中加入附加项目,解决无法打开包括文件:“extensions/ExtensionMacros.h”: No such file or directory”
- mysqld_safe --skip-grant&提示mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists
- libxml/tree.h no such file or directory
- “extensions/ExtensionMacros.h”: No such file or directory
- Cocos2d-x 3.0 beta 中加入附加项目,解决无法打开包括文件:“extensions/ExtensionMacros.h”: No such file or directory”
- 利用FileChannel复制文件 Copy one File to Another【三种方法】
- 无法打开包括文件:“extensions/ExtensionMacros.h”: No such file or directory
- You must specify at least one directory for a flat directory repository.
- git tip: How to undo an uncommitted change/deletion for a specific file in your working directory
- 解决ubuntu one for windows 无法同步问题- file sync starting
- QT:make: Nothing to be done for `first'和error:QtSql:No such file or directory
- libxml/tree.h no such file or directory
- Directory lookup for the file "xxx.mdf" failed with the operating system error 2
- Cocos2d-x 3.0 beta 中加入附加项目,解决无法打开包括文件:“extensions/ExtensionMacros.h”: No such file or directory”
- 【Qt开发】 QT:make: Nothing to be done for `first'和error:QtSql:No such file or directory
- use another thread loading file for other threads parse will not speed up application