如何在子文件夹中读取所有文本文件,然后将文件内容(四位数号码)添加到列表中?

发布于 2025-02-06 16:49:36 字数 498 浏览 2 评论 0原文

我正在学习Python,并且正在尝试读取我拥有的子文件夹中的所有文本文件,并将文件内容(四位数号码)添加到列表中。我只想读取文件扩展名.txt的文件。

我尝试过:

import os
path = "C:\python_projects\PythonProgramming\data-task1\q6" (example)
os.chdir(path)

def read_text_file(file_path):
    with open(file_path, 'r') as f:
        print(f.read())

for file in os.listdir():  
    if file.endswith(".txt"):
        file_path = f"{path}\{file}"
    
        read_text_file(file_path)

你能帮我吗?太感谢了。

I'm learning python, and I'm trying to read all text files in the subfolders I have, and add the file content (a four-digit number) to a list. I only want to read files with a file extension .txt.

I tried:

import os
path = "C:\python_projects\PythonProgramming\data-task1\q6" (example)
os.chdir(path)

def read_text_file(file_path):
    with open(file_path, 'r') as f:
        print(f.read())

for file in os.listdir():  
    if file.endswith(".txt"):
        file_path = f"{path}\{file}"
    
        read_text_file(file_path)

Can you please help me? Thank you so much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩于岁月 2025-02-13 16:49:36

从您当前的代码中,您应该只添加列表和转换。

    import os
    path = "C:\python_projects\PythonProgramming\data-task1\q6" # Example.
    os.chdir(path)
    
    def read_text_file(file_path):
        with open(file_path, 'r') as f:
            return f.read()
    
    numbers = []
    for file in os.listdir():  
        if file.endswith(".txt"):
            file_path = f"{path}\{file}"
    
            numbers.append(int(read_text_file(file_path)))

From your current code, you should just add the list and conversion like so:

    import os
    path = "C:\python_projects\PythonProgramming\data-task1\q6" # Example.
    os.chdir(path)
    
    def read_text_file(file_path):
        with open(file_path, 'r') as f:
            return f.read()
    
    numbers = []
    for file in os.listdir():  
        if file.endswith(".txt"):
            file_path = f"{path}\{file}"
    
            numbers.append(int(read_text_file(file_path)))
北陌 2025-02-13 16:49:36

我建议使用一个函数,可以递归地使用所有文件夹和子文件夹

from os import listdir, chdir
from os.path import isdir

path = r"C:\users\me\whatever\path"
chdir(path)
numbers = []

def read_text_file(file_path):
    with open(file_path, "r") as f:
        return f.read()

def checkFolder(path):
    for file is listdir(path):
        if file.endswith(".txt"):
            file_path = f"{path}\{file}"
            numbers.append(int(read_text_file(file_path)))
        elif isdir(f"{path}\{file}"): #Checks if the file is a directory
            checkFolder(f"{path}\{file}") #Re-run the function with the next subfolder

checkFolder(path)

I'd recommend using a function, that can be used recursively to go through all folders and subfolders

from os import listdir, chdir
from os.path import isdir

path = r"C:\users\me\whatever\path"
chdir(path)
numbers = []

def read_text_file(file_path):
    with open(file_path, "r") as f:
        return f.read()

def checkFolder(path):
    for file is listdir(path):
        if file.endswith(".txt"):
            file_path = f"{path}\{file}"
            numbers.append(int(read_text_file(file_path)))
        elif isdir(f"{path}\{file}"): #Checks if the file is a directory
            checkFolder(f"{path}\{file}") #Re-run the function with the next subfolder

checkFolder(path)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文