简体   繁体   English

python-如何使用python列出目录中每个文件的名称?

[英]How to carry in a list the name each file from a directory with python?

I am applying some regex to a folder full of .txt files in order to extract some specific patterns like this: 我正在将一些正则表达式应用到一个充满.txt文件的文件夹中,以提取一些特定的模式,例如:

def retrive(directory, a_regex):
    for filename in glob.glob(os.path.join(directory, '*.txt')):
        with open(filename, 'r') as file:
            important_stuff = re.findall(a_regex, file.read(), re.S)
            my_list = [tuple([j.split()[0] for j in i]) for i in important_stuff]
            print my_list


lists_per_file = retrive(directory,regex_)

And the output is the desired content of all the files in a list: 输出是列表中所有文件的所需内容:

[interesting stuff 1]
[interesting stuff 2]
[interesting stuff 3]
...
[interesting stuff n]
[interesting stuff n-1]

How can I carry or bind to the list the name of each document file, ie something like this: 如何将每个文档文件的名称携带或绑定到列表,例如:

[interesting stuff 1], name_of_document_1
[interesting stuff 2], name_of_document_2
[interesting stuff 3],name_of_document_3
...
[interesting stuff n], name_of_document_n
[interesting stuff n-1], name_of_document_n-1

Thanks in advance guys. 在此先感谢大家。

If you want to print the list and then the filename without a newline between the two, you will first have to turn the list into a string, then strip off the brackets from around the list. 如果要打印列表,然后再打印文件名,且两者之间没有换行符,则必须先将列表转换为字符串,然后再删除列表中的括号。 After that you can grab the filename from the filepath that you have, and put the two together. 之后,您可以从已有的文件路径中获取文件名,然后将两者放在一起。

See code below; 参见下面的代码;

def retrive(directory, a_regex):
    for filename in glob.glob(os.path.join(directory, '*.txt')):
        with open(filename, 'r') as file:
            important_stuff = re.findall(a_regex, file.read(), re.S)
            my_list = [tuple([j.split()[0] for j in i]) for i in important_stuff]
            # print my_list # old line
            print str(my_list).strip('[]') + filename.split('/')[-1]


lists_per_file = retrive(directory,regex_)

暂无
暂无

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

相关问题 如何使用目录的每个子目录中的文件数创建python列表 - How to create a python list with the number of file in each sub directory of a directory 从 Python 中的目录内容列表中获取文件名 - Get file name from list of directory contents in Python 如何将变量从导入的函数携带到主文件(Python 3.8.3 - How to carry variables from imported functions to the main file (Python 3.8.3 python 2.7 Tk更改标签以从打开的文件中携带文件名 - python 2.7 Tk change label to carry file name from opened file 如何打印每个文件的名称以及目录中的内容? - How to print name of each file , and content in directory? 如何获取子目录名称列表、每个目录中的文件名以及每个目录的路径(Python) - how to get a list of sub directory names, the file names in each and the paths to each of these (Python) 如何从 python 中的目录中获取图像文件名 - how to get the image file name from directory in python 如何在不知道python中每个文件名的情况下访问目录中的多个文件 - How to access more than one file in a directory without knowing the name of each file in python 从CSV文件导出文件名列表并使用python复制到另一个目录 - Export the list of file name from CSV file and copy to another directory with python Python:如何返回 os.walk 返回的每个文件的父目录名称? - Python: How do I return the parent directory name of each file os.walk returns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM