【十一月的萧邦】

蛋蛋说我写的博客叫做情感博客,所以现在不忍心,出来水一篇,毕竟为了庆祝十一月的到来。
十一月有周杰伦的新专辑,当时我们买了藏在书包里,开运动会,一本漫画,一副耳机,躺在绿油油的草地上,哎,小姐,请问有没有卖半岛铁盒,走廊灯关上,恩恩听不清,我走到窗边书包放。。。。
感觉这些就是整个世界。

这个其实叫做写在前面的话,因为现在是十一月的第一天,小时候超级喜欢十一月,因为每个运动会都会开在十一月,然后昆明的十一月的气候超级好,天空很蓝,空气微微冷。每次开运动会都可以偷偷跑出学校门口买好吃的。小伙伴会把自己家的狗狗带到学校,spirit我还记得她的狗狗叫做这个名字。

小时候我可是运动健将,因为没有人的仰卧起坐能够比过我,跳远也很厉害,然后每次都可以拿奖。但是拿奖的时候总是很尴尬,因为我的名字很少见,主持人喜欢把我的名字分开念,生气。我还记得杨龙当时笑的嘴都快裂开了。大家全部笑的趴在地上,生气!!!


哈哈,好的开始写技术部分,今天的内容叫做批处理修改文件的名字。当然我们是不可能讲这么简单的东西的,只是通过这个简单的东西我们讲怎么一点点的改进自己的代码,实现代码的高效复用,也就是传说中的造轮子。把自己的代码改造的更加的完备。更加的实用,写一些值得收藏的代码。


1.运用场景

假设我们有一批文件经过某些加工之后名字变了,但是我们想按照自己的意愿改名字,在Python中给我们提供了一个强大的轮子,叫做os.rename(oldname,newname)
通过调用这个函数呢,我们可以轻易的实现改名字。

为了更加形象的说明我们的问题,我们来看几张图片吧。

比如说在某个文件夹的下面我们存在着一些文件,就像上面这张图片一样,在-前面的是我们文件本来的名字,经过了某些处理之后我们的文件变成了上面的这个样子,我们只想保留文件原来的名字,就像下面这张图一样。这个时候我们就需要做一个批处理,来修改我们这些文件的名字。

使得它们变成上面的这个样子。


2.实现思路

根据需要的需求,我们可以设计出我们的实现思路。
1.我们发现我们只要“-”这个符号前面的文件名,之后在加上文件的文件类型,后缀,那么就是我们文件最终的名字。
2.我们知道文件的名字其实是字符串,在字符串中我们可以根据split() 这个函数来进行分割,我们根据split("-") 这个函数得到的是一个List,我们只需要去List的第一个元素就是我们文件名,之后加上后缀,然后使用我们Python自带的函数,我们就可以实现文件名的重新命名。

3.其实我之前写过一个类似的功能,就是从APK中提取出我们的classes.dex文件和我们的so文件

那个难度比这个大一点。我们用到了zip文件的读取和写入,当然还有其他的实现思路。


3.代码实现

讲完了我们的运用场景,实现思路,是不是觉得我们的代码实现会比较简单呢。OK,那么我们就来写一个简单的代码。

我们先来实现一下一个比较简单的版本:


代码复制版本:

#!/usr/bin/env python
# coding=utf-8
# @author    :  Chicho
# @version   :  1.0
# @date      :  2016-11-1 11:00
# @ function :  Batching rename the name of file# @running   :  python renameGator.pyimport os
#path储藏了我们需要处理文件的路径,假设我的路径就叫这个
path = "/home/chicho/test/Out/"# 之后我们或许文件列表使用的是listdir()这个函数,我们需要
#引入 os --->import os 才能使用
# 下面的图形为我们展示了这个函数的运行结果
fileList=os.listdir(path)for f in fileList:filePath = os.path.join(path,f)
# 根据紧接着的图片我们可以发现,里面含有文件夹,还有文件,我们
#需要处理的只是文件,不是文件夹
#所以我们需要isfile来判断一下if os.path.isfile(filePath):portion = f.split("-")[0]# 获取文件名的前半部分#加上后缀newName = portion + ".xml"#新文件的路径#如果代码和文件不在同一个目录下必须这么修改#否则生成的文件就会在里代码运行的目录下面newNamePath=os.path.join(path,newName)
#重命名os.rename(filePath,newNamePath)print "new we are handleding the {0}".format(newName)print "all work is done!"

OK,我们 就这么搞定了我们的代码。


3.1 代码升级

当然了,这种是属于轮子型的代码,我们需要在扩展一下,让它能够更加的智能。比如说路径先不要固定死,我们可以自己输入。下面我们再来个升级版本。

我们在运行的时候就直接把参数传给我们的程序,也就是我们需要处理的路径

#!/usr/bin/env python
# coding=utf-8
# @author    :  Chicho
# @version   :  2.0
# @date      :  2016-11-1 11:00
# @ function :  Batching rename the name of file# @running   :  python renameGator.py
# @running   :  python renameGator.py you_file_path
#            :  e.g: python renameGator.py /home/chicho/test/Out/import os
import sysdef Usage():print "usage:"print "python renameGator.py your_file_path\n"if len(sys.argv)==1:path = "/home/chicho/test/sootAndroidOut/"
elif len(sys.argv)==2:path = sys.argv[1]if not os.path.exists(path):print "\nyou input is wrong, cannot find the path!"print "Please check you path!\n"Usage()sys.exit(1)fileList=os.listdir(path)for f in fileList:filePath = os.path.join(path,f)if os.path.isfile(filePath):portion = f.split("-")[0]newName = portion + ".xml"newNamePath=os.path.join(path,newName)os.rename(filePath,newNamePath)print "new we are handleding the ****++ {0} ++****".format(newName)print "all work is done!"

当然了这个只是一个简答 的Demo,还有不完善的地方,就比如说我们参数处理部分还是不完善。还需要再增加一个判断条件,防止用户的错误输入。

下面我们再把逼格提升一点。写的更加像程序员该做的事情。

#!/usr/bin/env python
# coding=utf-8
# @author    :  Chicho
# @version   :  3.0
# @date      :  2016-11-1 11:00
# @ function :  Batching rename the name of file# @running   :  python renameGator.py
# @running   :  python renameGator.py you_file_path
#            :  e.g: python renameGator2.py /home/chicho/test/sootAndroidOut/
# @running   :  python renameGator2.py -h
# @running   :  python renameGator2.py -i your_file_path
#               e.g: python renameGator2.py /home/chicho/test/sootAndroidOut/  import os
import sys,getoptdef Usage():print "usage:"print "python renameGator.py your_file_path\n"def usage1():print "usage:"print "python renameGator2.py -i your_file_path"#********************************opts,args = getopt.getopt(sys.argv[1:],"hi:")for op,value in opts:if op == "-i":path = valueif not os.path.exists(path):print "Cannot find the Path!"sys.exit(1)elif op == "-h":usage1()sys.exit(0)if len(sys.argv)==1:path = "/home/chicho/test/sootAndroidOut/"
elif len(sys.argv)==2:path = sys.argv[1]if not os.path.exists(path):print "\nyou input is wrong, cannot find the path!"print "Please check you path!\n"Usage()print "or"usage1()sys.exit(1)fileList=os.listdir(path)i=0
for f in fileList:filePath = os.path.join(path,f)if os.path.isfile(filePath):portion = f.split("-")[0]newName = portion + ".xml"newNamePath=os.path.join(path,newName)os.rename(filePath,newNamePath)print "new we are handleding the ****++ {0} ++****".format(newName)i = i+1print "There are {0} files being handled!".format(i)
print "all work is done!"

用户在不知道怎么运行代码的情况下我们可以通过运行 -h来得到帮助。

但是这代码的容错能力还是一般,要是有的文件当中根本就没有“-”,那么我们就只需要修改需要改名字的文件。

比如上面这个我们不需要处理apv.xml这个文件
如果要用上面这个代码我们发现处理的结果就会有问题。
所以我们再来改进一下我们的代码

#!/usr/bin/env python
# coding=utf-8
# @author    :  Chicho
# @version   :  4.0
# @date      :  2016-11-1 11:00
# @ function :  Batching rename the name of file# @running   :  python renameGator.py
# @running   :  python renameGator.py you_file_path
#            :  e.g: python renameGator3.py /home/chicho/test/sootAndroidOut/
# @running   :  python renameGator3.py -h
# @running   :  python renameGator3.py -i your_file_path
#               e.g: python renameGator2.py /home/chicho/test/sootAndroidOut/  import os
import sys,getoptdef Usage():print "usage:"print "python renameGator.py your_file_path\n"def usage1():print "usage:"print "python renameGator2.py -i your_file_path"#********************************opts,args = getopt.getopt(sys.argv[1:],"hi:")for op,value in opts:if op == "-i":path = valueif not os.path.exists(path):print "Cannot find the Path!"sys.exit(1)elif op == "-h":usage1()sys.exit(0)if len(sys.argv)==1:path = "/home/chicho/test/sootAndroidOut/"
elif len(sys.argv)==2:path = sys.argv[1]if not os.path.exists(path):print "\nyou input is wrong, cannot find the path!"print "Please check you path!\n"Usage()print "or"usage1()sys.exit(1)fileList=os.listdir(path)i=0
for f in fileList:filePath = os.path.join(path,f)if os.path.isfile(filePath):if not "-" in f:continueportion = f.split("-")[0]newName = portion + ".xml"newNamePath=os.path.join(path,newName)os.rename(filePath,newNamePath)print "new we are handleding the ****++ {0} ++****".format(newName)i = i+1print "There are {0} files being handled!".format(i)
print "all work is done!"

运行一下看看处理的结果



当然了,这个是个轮子的代码,为了能方便广大人民也能使用这个代码,我们就把这个代码改装成轮子。

我们把这个代码写入到一个叫做rename.py 的代码中,改造成轮子。

#!/usr/bin/env python
# coding=utf-8
# @author    :  Chicho
# @version   :  1.0
# @date      :  2016-11-1 11:00
# @ function :  Batching rename the name of file# @running   :  python rename.pyimport os def renameFile(path):fileList=os.listdir(path)for f in fileList:filePath = os.path.join(path,f)if os.path.isfile(filePath):portion = f.split("-")[0]newName = portion + ".xml"newNamePath=os.path.join(path,newName)os.rename(filePath,newNamePath)print "new we are handleding the {0}".format(newName)print "all work is done!"

怎么调用呢,

看下面的代码:

#!/usr/bin/env python
# coding=utf-8
# @author   : Chicho
# @version  : 1.0
# @date     : 2016-11-1 13:45
# @function : Batching rename the name of files
# @running  : python invoke.pyimport renamepath="/home/chicho/test/sootAndroidOut/"#rename.renameFile(path)if __name__== "__main__":rename.renameFile(path)

那么,我们就把整个功能都实现了。






写在后面的话

好好学习,天天向上
你必须非常努力,才可以看起来毫不费力

不要忘了做过的梦
shining,shinging
每当有泪儿流
就回到那个宇宙
天上星星,仿佛听她诉说
骄傲的闪不停

python批处理修改文件的名字相关推荐

  1. python改文件名_通过python顺序修改文件名字的方法

    通过python顺序修改文件名字的方法 更新时间:2018年07月11日 11:48:55 作者:longma666666 今天小编就为大家分享一篇通过python顺序修改文件名字的方法,具有很好的参 ...

  2. python批量化修改文件名字(带窗口,可调整设置)

    python批量化修改文件名字(带窗口,可调整设置) 功能描述 注意事项 效果图 源代码 功能描述 主要有以下几个 1.直接修改文件名字,并在名字后面添加数字进行排序. 2.在文件名字前面增加前缀. ...

  3. bat批处理修改文件夹下文件名字

    bat批处理修改文件夹下文件名字 业务场景 有时候我们经常会遇到需要批量处理一些文本或者文件名称的问题,批量文本的处理可以使用notepad++ 里面的列编辑,或者批量替换字符串即可.假如有这样一个要 ...

  4. python批量修改文件扩展名

    python批量修改文件扩展名录 前言 代码如下 前言 利用python将文件夹里的.txt文件修改为.tif文件. 代码如下 import os dir='/home/下载/'#文件所在目录 fil ...

  5. 【python】修改文件后缀,将JPG转换为PNG/PNG转换为JPG

    [python]修改文件后缀,将JPG转换为PNG/PNG转换为JPG 前言 os.rename replace 前言 本文只要有两种方法实现将JPG格式图片转换为PNG,第一种为应用os.renam ...

  6. sudo修改文件夹名字_用 Python 高效智能管理文件夹

    #「闪光时刻」主题征文 二期# 大家在写报告.写总结时,是否会先去翻一下以前写过的类似的东西?是否有看过比较好的文章,想保存时却为归类而纠结?是否电脑里的文件越来越多,想删掉一些却又舍不得?身处大数据 ...

  7. python批量修改文件名字为数字编号

    控制台进入python环境 win+R---->cmd----->python import os#输入待修改文件夹路径 path="E:/pyexe/exe/test/&quo ...

  8. sudo修改文件夹名字_linux

    Linux下快速批量修改文件夹下的图片名称的方法 Linux下快速批量修改文件夹下的图片名称的方法 我们都知道,要修改文件夹下的图片名称很容易,但是要批量修改就比较浪费时间了,那么有什么方法能够快速批 ...

  9. Python批量修改文件夹及其子文件夹下的文件内容

    前言:前几天我看一位同学要修改很多文件中的数据,该文件数据很规律,一行只有三个数,需要将每行最后一个数字改为负数,但文件有上千个,分布在每个文件夹下面以及它的多级子文件夹下,看他用excel手动改数据 ...

最新文章

  1. Windows Azure Storage (25) Azure Append Blob
  2. SpringBoot笔记:SpringBoot2.3集成SpringSession+nginx+redis实现session共享
  3. cocos2d-x游戏实例(25)-简易动作游戏(3)
  4. linux修改ftp锁定目录,解决linux下ftp指定访问目录无法修改的问题
  5. Feature Tools:可自动构造机器学习特征的Python库
  6. 站在巨人肩上的.NET Core 2.1
  7. fifo的valid信号啥时候为高_五角枫啥时候适合移栽?五角枫移栽最佳时间
  8. 【luogu4185】 [USACO18JAN]MooTube [并查集]
  9. 面试题(4)--基础篇
  10. 关键路径c语言,有向图的关键路径的C程序实现代码
  11. 链表的基本操作——学生信息管理表
  12. MATLAB数值计算——矩阵的逆、矩阵的特征值、矩阵的特征多项式
  13. 查询中国天气网api需要用到的城市代码
  14. bind9智能dns配置
  15. 【51单片机STC89C52】LCD1602液晶屏的使用
  16. 在Linux中,如何找回root密码(So easy!!)
  17. 转载的一片关于Mapper.xml中sql的相关技术点,供以后自己慢慢学习之用
  18. Learning to Rank(以下简称L2R)
  19. Debian 8桌面安装Nvidia GTX960显卡驱动
  20. 远程连接kali linux一直提示密码错误的解决方法

热门文章

  1. 架构篇:什么才是真正的架构设计?
  2. 秒杀系统怎么设计?8张图带你搞定!
  3. 新同事说工厂模式有啥用,别学了
  4. 低成本可复用前端框架——Linke
  5. 某互联网大厂出现招聘事故!HR告知应聘者肯定会发offer,应聘者拒绝另一家公司耐心等待,hr却说流程有变,offer被卡!...
  6. 标量子查询产生的SQL性能瓶颈,该怎么合理优化?
  7. 写代码做副业月入33K+的方法都藏在这几个淘宝大佬的公众号里!
  8. TiDB 在小红书从 0 到 200+ 节点的探索和应用
  9. 手机调试打开控制台方法vconsole
  10. 07 Java面试反射原理