python - 删除文件名中任何位置包含数字的python文件

标签 python

我有一个包含 .jpg 文件的目录,我需要删除此目录中文件名中任意位置包含数字的所有文件。

我使用 os.listdir 来指定路径。然后我可以使用 fnmatch.fnmatch 只删除特定的东西,比如“3453.jpg”,但我需要它来删除像 thomas9.jpg 或 thom43as.jpg 这样的东西。基本上任何包含数字的文件名都需要删除。

谢谢, 汤姆

最佳答案

使用re 模块。

import os
import re


for _file in os.listdir('.'):
    if re.search('\d.*\.jpg$', _file):
        print(_file)

关于python - 删除文件名中任何位置包含数字的python文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34478392/

相关文章:

python - 编写CSV文件Python3问题

python unicode 正则表达式

python - 当我在 Python 中使用命令时,命令的运行与预期不同

python - 如何使用 NumPy.recarray 的两个 View 修改它

python - keras 中的图像到图像映射

python - Flask 可插入 View 和需要登录

python - 如何将 Networkx 中的两条边和节点组合成一个具有公共(public)起始节点的边和节点?

python - TypeError : string argument without an encoding

python - MATLAB 中的连续插值?

python - 如何在 Python 的绘图轴上操作日期时间(刻度标签和限制)?