python - 如何从文件名中获取数字?

标签 python python-2.7

根据 key ,我在目录中有很多文件:

pic001.jpg
pic002.jpg
pic012.jpg
[...]
ico001.jpg
ico002.jpg
ico012.jpg
[...]

我想列出这些文件并创建这样的结构:

for r,d,f in os.walk(directory):
    for file in f:
        if file.startswith("pic"):
            pic = file
            ico = ???
            images_list.append({
                'big': directory + '/' + pic,
                'thumb': directory + '/' + ico,
            })

如何获取给他分配的“pic”文件和“ico”(前提是ico存在)?

最佳答案

最简单的答案似乎是:

ico = 'ico' + file[3:]

关于python - 如何从文件名中获取数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16544923/

相关文章:

python - ZipFile 不在存档文件内存储任何文本

python - 检查给定的输入是否是有效的 IP 或主机名或无效的内容

python - 用于测试的 Scrapy 限制请求

python - 在长时间运行的任务中终止 QThread 和 multiprocessing.pool 进程

python-2.7 - python peewee - 如何使用不同的

python - 格式化列表python的元素

python - 带有 C 指针的 Pickle Cython 类

Python - 'get_sheet_by_name' 错误

python - "\b"字符串中行为不当

Python正则表达式匹配: ## ##