简体   繁体   English

有没有办法创建一个函数来加载目录中的所有数据文件,然后输出他们的文件名和内容?-Python 3

[英]Is there a way to create a function to load all data files in directory and then output their file name and content?-Python 3

Is there a way to create a function to load all data files in directory and then output their file name and content? 有没有办法创建一个函数来加载目录中的所有数据文件,然后输出他们的文件名和内容?

Input: Get all files in a given directory of mine (wow.txt, testting.txt, etc.) 输入:获取我的给定目录中的所有文件(wow.txt,testting.txt等)

Process: I want to run all the files through a function 进程:我想通过一个函数运行所有文件

Output: I want the output to be the total number of files processed and all the files names and their respective content below it. 输出:我希望输出为处理的文件总数以及它下面的所有文件名及其各自的内容。

For example: 例如:

Total Number of Documents: 6

/home/file/wow.txt

"all of its content"

/home/file/www.txt

"all of its content"

Here is my code: 这是我的代码:

#Import Functions  
import glob
# get all the .txt files
files=glob.glob("*.txt")  

#Load Data Function
def load_data(files):
    """
    Input  : path to all .txt files
    Purpose: loading all text file
    Output : list of documents along with their respective content 

    """

    documents_list=[]
    content=[]

    for file in files:
        with open(file,"rt",encoding="latin-1") as fin:
            print(file)
            for line in fin.readlines():
                text = line.strip()
                documents_list.append(text)
        print("Total Number of Documents:",len(documents_list))
        content.append( text[0:min(len(text),100)])
    return documents_list,content

#Output
load_data(files)

Here is my output: 这是我的输出:

在此输入图像描述

在此输入图像描述

  1. As you can see in the first part of the output, it's showing each file and random number. 正如您在输出的第一部分中看到的那样,它显示了每个文件和随机数。 Instead it should just have the total number of documents (which is 5) 相反它应该只有文件总数(5)

  2. It shows the content of all the files but it doesn't separate them by file. 它显示所有文件的内容,但不会按文件分隔它们。 As you can see by the red line, that shows the end of the first file and below the red line is the start of another. 正如您可以通过红线看到的那样,显示第一个文件的结尾,红色线下方是另一个文件的开头。

Any suggestions? 有什么建议么?

def print_files_in_directory(directory):
    files = [f for f in os.listdir(directory) if os.path.isfile(f)]
    print(f'Total Number of Documents: {len(files)}')
    for f in files:
        file_path = os.path.join(directory, f)
        print(file_path)
        print('\n')
        with open(file_path, 'r') as fp:
            print(fp.read())

If you want it to include files in subdirectories, you'll either have to manually recurse those subdirectories yourself or use os.walk() 如果您希望它包含子目录中的文件,您必须自己手动递归这些子目录或使用os.walk()

暂无
暂无

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

相关问题 有没有一种方法可以使用Python从目录中的所有文件中加载数据? - Is there a way to load data from all files in a directory using Python? 在python中,如何在目录中输出具有相同扩展名的所有文件名? - In python,how to output all the files name with the same extension in a directory? 文件运行后在目录中的所有文件上运行 Python 脚本,创建一个新文件夹并以该文件命名该文件夹 - Run Python script on all files in the directory after a file has run, create a new folder and name the folder after the file Python:在目录中创建以“.json”结尾的所有文件的zip文件 - Python: Create a zip file of all files ending with “.json” in a directory Python - 读取文件夹中的所有文件,并为每个文件创建唯一的输出 - Python - Read all files in a folder and create unique output for each file 循环以根据名称与行内容相同的条件创建具有特定文件名和内容的输出文件 - loop to create output files with specific file names and content based on condition where name will be same as row content 如何在python中读取目录的所有文件并将文件数据保存在字典中 - How to read all the files of a directory in python and save file data in a dictionary 读取目录中的所有文件并输出到单个文件 - Read all files in a directory and output to a single file 在目录python中搜索所有具有相同名称的文件 - Search all files with same name in a directory python 在python中的目录和子目录中创建所有文件的列表 - Create a list of all files in directory and subdirectories in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM