在文件夹中搜索包含子字符串的文件,python? [英] search for a file containing substring in a folder, python?

查看:70
本文介绍了在文件夹中搜索包含子字符串的文件,python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设路径为"c:\ users \ test",文件夹"test"包含许多文件.我想在测试文件夹中搜索文件,该文件名在python脚本中包含单词"postfix".有人可以帮我吗?

Suppose the path is "c:\users\test" , the folder "test" contains many files. i want to search for a file in test folder ,file name containing a word "postfix" in it in python script. Can anybody help me with it?

推荐答案

glob模块内置于python就是为此而制作的.

The glob module builtin to python is made exactly for this.

import glob
path_to_folder = "/path/to/my/directory/"
matching_files = glob.glob(path_to_folder+"*postfix*")
for matching_file in matching_files:
    print(matching_file)

应打印出所有包含后缀"的文件,其中*是与任何内容匹配的通配符.因此,此模式将匹配test_postfix.csvmypostfix.txt

should print out all of the files that contain "postfix" the * are wildcard characters matching anything. Therefore this pattern would match test_postfix.csv as well as mypostfix.txt

这篇关于在文件夹中搜索包含子字符串的文件,python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆