Python3 文件批量重命名操作示例!
在Python中os模块里,os.renames() 方法用于递归重命名目录或文件。类似rename()。
rename()方法语法格式如下:
os.rename(old,new)
Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python
old是需要修改的目录/文件名,new是修改后的目录/文件名,通过这个方法我们可以很轻松的完成批量在文件/目录增加固定前缀或者批量删除文件/目录固定前缀 。
以下代码Windows下和Linux都可以使用。
示例如下:
增加前缀’[Linuxidc.]’:
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import os
path='/home/linuxidc/linuxidc.com'
old_dir=os.listdir(path) #获取/home/linuxidc/linuxidc.com目录下的所有文件目录
print("原始目录为 %s"%old_dir)
for i in old_dir:
new_name='[Linuxidc.]'+i
os.rename(i,new_name)
new_dir=os.listdir(path)
print("现在的目录为%s"%new_dir)
</pre>
运行后输出如下:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1559544289443" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
删除前缀’[Linuxidc.]’:
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import os
path='/home/linuxidc/linuxidc.com'
old_dir=os.listdir(path)
print("原始目录为 %s"%old_dir)
for i in old_dir:
shu=i.rfind(']') #获取到']'的位置
new_name=i[shu+1:]
os.rename(i,new_name)
new_dir=os.listdir(path)
print("现在的目录为%s"%new_dir)
</pre>
运行后输出如下:
原始目录为 ['[Linuxidc.]linuxidc.com.sh', '[Linuxidc.]kakoune', '[Linuxidc.]linuxidc', '[Linuxidc.]linuxidc.Java', '[Linuxidc.]linuxidc.com.jpg', '[Linuxidc.]opencv-3.4.0', '[Linuxidc.]linuxidc.class', '[Linuxidc.]Linux公社www.linuxidc.com.py', '[Linuxidc.]linuxidc.com.txt', '[Linuxidc.]linuxidc.txt', '[Linuxidc.]linuxidcbuild', '[Linuxidc.]amp', 'www.linuxidc.com.py', '[Linuxidc.]linuxidc.sh', '[Linuxidc.]linuxisdc.com.jpg', '[Linuxidc.]www.linuxidc.com', '[Linuxidc.]linuxidc.com.png', '[Linuxidc.].vimrc', '[Linuxidc.]tupian']
现在的目录为['linuxidc.txt', 'www.linuxidc.com', 'linuxidc.java', 'linuxidc.com.png', 'amp', 'Linux公社www.linuxidc.com.py', 'linuxidc.class', 'linuxisdc.com.jpg', 'linuxidc', 'opencv-3.4.0', 'www.linuxidc.com.py', 'linuxidc.com.sh', '.vimrc', 'linuxidcbuild', 'kakoune', 'tupian', 'linuxidc.sh', 'linuxidc.com.txt', 'linuxidc.com.jpg']
[Finished in 0.1s]
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1559544289450" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
在Windows下还需要注意一下文件路径应使用双斜杠// ,否则会出现错误,Linux下一般不会出现此错误。