温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python批量修改文件名(删除指定关键字)

发布时间:2020-08-30 08:05:24 来源:网络 阅读:974 作者:badboychcj 栏目:编程语言

因下载的视频文件大多数含有视频网站的url或者包含其他不要的字符串,用python自动修改。


目前缺点:

1,需要把.py放在目录内运行


代码如下:

import os, re

while True:
    keyword = input("请输入你要删除的字符串:")
    if len(keyword)==0 or keyword.isspace():
        print("字符串不能为空!")
    else:
        break

suffix = input("需要筛选的文件名后缀(Enter代表所有):")

fileNames = os.listdir()  #获取当前目录下的所有文件

for file in fileNames:
    check = os.path.join(os.path.abspath('.'),file)
    if os.path.isfile(check):
        if len(suffix)==0 or suffix.isspace():
            if keyword in file:
                print(file," -> ",file.replace(keyword,''))
                os.rename(file,file.replace(keyword,''))
            else:
                #用正则表达式匹配后缀名
                if re.match('.+?\.'+suffix+'$',file) != None and keyword in file:
                    print(file," -> ",file.replace(keyword,''))
                    os.rename(file,file.replace(keyword,''))


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI