python批量改名

20 篇文章 1 订阅
12 篇文章 0 订阅

python批量改名

非递归版

import os,sys

def update_files_name():
    old_names = os.listdir(path)
    for old_name in old_names:
        if old_name!= sys.argv[0]: # 代码本身路径,防止脚本文件放在path路径下时,被一起重命名
            if old_name.endswith('.mp4'):
                #print(old_name.replace('【研学仔20.21全年更新群666赠送】',''))
                new_name = old_name.replace('【研学仔20.21全年更新群666赠送】','')
                os.rename(os.path.join(path,old_name),os.path.join(path,new_name))
                print(old_name,"has been renamed successfully! New name is :",new_name)

if __name__ == '__main__':
    path = r'F:/BaiduNetdiskDownload/方程/'
    update_files_name()

递归版

import os,sys,re

def rename_files(path):
    lsdir = os.listdir(path)
    dirs = [i for i in lsdir if os.path.isdir(os.path.join(path, i))]
    if dirs:
        for i in dirs:
            rename_files(os.path.join(path, i))
    files = [i for i in lsdir if os.path.isfile(os.path.join(path,i))]
    
    endswith1 = [".mp4",".pptx",".zip",".rar",".pdf",".png",".doc",".docx",".xlsx",".conf",".java",".txt",".htm",".mht",".html",".dll",".xmind"]
    endswith2 = [".jar",".jmx",".tmp",".log",".gitignore",".xml",".yml",".properties",".md",".xls",".iml",".class",".bin",".cmd",".jpg"]   
    endswith_tuple = tuple(endswith1)

    for old_name in files:
       #print(os.path.join(path, old_name))
       if old_name.endswith(endswith_tuple): 
            #print(old_name)
            new_name = old_name.replace('【瑞客论坛 www.ruike1.com】','')
            new_name = new_name.replace('【瑞 客 论 坛 www.ruike1.com】','')            
            new_name = new_name.replace('【瑞  客论坛 www.ruike1.com】','')
            new_name = new_name.replace('【瑞  客论 坛 www.ruike1.com】','')
            new_name = new_name.replace('【瑞客论 坛 www.ruike1.com】','') 
            new_name = new_name.replace('【瑞客   论 坛 www.ruike1.com】','')
            new_name = new_name.replace('【瑞 客论 坛 www.ruike1.com】','')  
            new_name = new_name.replace('【瑞客论    坛 www.ruike1.com】','')     
            new_name = new_name.replace('【瑞   客论 坛 www.ruike1.com】','')
            new_name = new_name.replace('【瑞客  论坛 www.ruike1.com】','')               
            #print(os.path.join(path,old_name))
            #print(os.path.join(path,new_name))
            os.rename(os.path.join(path,old_name),os.path.join(path,new_name))
            #print(old_name,"has been renamed successfully! New name is :",new_name) 

def print_files(path):
    lsdir = os.listdir(path)
    dirs = [i for i in lsdir if os.path.isdir(os.path.join(path, i))]
    if dirs:
        for i in dirs:
            print_files(os.path.join(path, i))
    files = [i for i in lsdir if os.path.isfile(os.path.join(path,i))]
    for old_name in files:
       if old_name.endswith('.mp4'): 
            print(old_name)

if __name__ == '__main__':
    path = r'H:\2021工程师资料'
    #print_files(path)
    rename_files(path)
    #print_files(path)



参考python批量改名

要用到 os 模块下的几个方法

1、os.listdir(‘/root/python/’) #列出当前目录下所有文件
2、os.path.isdir(‘/abc’) #判断是否是目录,返回布尔值,不存在也返回false
3、os.path.isfile(‘/etc/passwd’) #判断是否是文件
4、os.path.join(‘/etc/’, ‘passwd’) #连接文件,返回/etc/passwd

改名并移动到指定目录

import os
import shutil


def mvFile(src,dst):
    fileType = [".png"]   
    endswith_tuple = tuple(fileType)
    #print('src:',src)
    srcPath = os.listdir(src)
    for old_name in srcPath:
        #print(old_name)
        #print('')
        if old_name.endswith(endswith_tuple): 
            src_path = os.path.join(src, old_name)
            des_path = os.path.join(dst, old_name)
            #print('src_path',src_path)
            #print('des_path',des_path)
            shutil.move(src_path, des_path)
        else :
            continue
    return
        
if __name__ == '__main__':
    src = "F:\\Baidu\\视频"
    dst = "F:\\Baidu\\视频\\video"
    
    for i in range(1,6) :
        srcDay  = 'day'+ str(i)
        srcDayPath = os.path.join(src, srcDay)
        videolist  = os.listdir(srcDayPath)
        print()
        print()
        print()
        for old_name in videolist:
            #print(srcDay,old_name)
            new_name = '0'+ str(i) + old_name
            #print(os.path.join(srcDayPath,old_name))
            #print(os.path.join(srcDayPath,new_name))
            os.rename(os.path.join(srcDayPath,old_name),os.path.join(srcDayPath,new_name))
            shutil.move(os.path.join(srcDayPath,new_name), os.path.join(dst,new_name))
            #print(os.path.join(dst,new_name))
        
    #mvFile(src,dst)

数字补零
使用python3的数字格式化方法。
下表展示了 str.format() 格式化数字的多种方法:

>>> print("{:.2f}".format(3.1415926))
3.14

表格

数字格式输出描述
3.1415926{:.2f}3.14保留小数点后两位
3.1415926{:+.2f}+3.14带符号保留小数点后两位
-1{:-.2f}-1.00带符号保留小数点后两位
2.71828{:.0f}3不带小数
5{:0>2d}05数字补零 (填充左边, 宽度为2)
5{:x<4d}5xxx数字补x (填充右边, 宽度为4)
10{:x<4d}10xx数字补x (填充右边, 宽度为4)
1000000{:,}1,000,000以逗号分隔的数字格式
0.25{:.2%}25.00%百分比格式
1000000000{:.2e}1.00e+09指数记法
13{:>10d}13右对齐 (默认, 宽度为10)
13{:<10d}13左对齐 (宽度为10)
13{:^10d}13中间对齐 (宽度为10)

详情参考Python format 格式化函数

查找并删除mp4文件

查找
将当前目录及其子目录下所有文件后缀为 .mp4 的文件列出来:

find . -name "*.mp4"

查找并删除

find . -name '*.mp4' -type f -print -exec rm -rf {} \;

分析:

(1) “.” 表示从当前目录开始递归查找

(2) “ -name ‘*.mp4’ "根据名称来查找,要查找所有以.mp4结尾的文件夹或者文件

(3) " -type f "查找的类型为文件

(4) “-print” 输出查找的文件目录名

(5) 最主要的是是-exec了,-exec选项后边跟着一个所要执行的命令,表示将find出来的文件或目录执行该命令。
exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{}一个空格一个斜扛,最后是一个分号.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值