python批量改名

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选项后面跟随着所要执行的命令或脚本,然后是一对儿{}一个空格一个斜扛,最后是一个分号.

你可能感兴趣的:(机器学习,深度学习,python,python,深度学习,人工智能,大数据)