简体   繁体   English

如何删除 python 中文件名中包含特定单词的文件

[英]How to delete a file with a specific word in the filename in python

To better illustrate what I'm trying to do, I download a iTunes file in m4a but it's encrypted by widevine so I decrypt it to get a DRM free m4a file为了更好地说明我正在尝试做的事情,我在 m4a 中下载了一个 iTunes 文件,但它是由 Widevine 加密的,所以我解密它以获得一个 DRM 免费的 m4a 文件

so in the download folder there is:所以在下载文件夹中有:

  • 8 - Bring Me The Horizon - Run.m4a 8 - 给我带来地平线 - Run.m4a
  • 8 - Bring Me The Horizon - Run_encrypted.m4a 8 - 给我带来地平线 - Run_encrypted.m4a

And my question is: is there a sort of algorithm who can filter files in my download folder with the word "encrypted" and delete them?我的问题是:有没有一种算法可以用“加密”这个词过滤我下载文件夹中的文件并删除它们?

You can do this using glob.您可以使用 glob 执行此操作。

import glob
import os

# path to search file
path = 'path_to_folder/*encrypted.m4a'
for file_path in glob.glob(path, recursive=True):
    print(file_path)
    os.remove(file_path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM