Python 如何打开和读取多个.txt文件

Python 如何打开和读取多个.txt文件,python,file,Python,File,这个问题以前有一位朋友问过,但我们没有得到答案 我们需要从我的目录中打开并读取35个扩展名为.txt的文本文件。打开和读取这些文件的目的是将所有文本按顺序放入一个文件中。文件从1到35枚举,例如Chapter1.txt、Chapter2.txt…Chapter35.txt 我已尝试循环浏览目录中的所有文件,打开并读取这些文件以附加一个列表,但我始终收到一条错误消息,该消息毫无意义,因为所有文件都在目录中: Traceback (most recent call last): File "/U

这个问题以前有一位朋友问过,但我们没有得到答案

我们需要从我的目录中打开并读取35个扩展名为.txt的文本文件。打开和读取这些文件的目的是将所有文本按顺序放入一个文件中。文件从1到35枚举,例如Chapter1.txt、Chapter2.txt…Chapter35.txt

我已尝试循环浏览目录中的所有文件,打开并读取这些文件以附加一个列表,但我始终收到一条错误消息,该消息毫无意义,因为所有文件都在目录中:

Traceback (most recent call last):
  File "/Users/nataliaresende/Dropbox/PYTHON/join_files.py", line 
27, in <module>
    join_texts()
  File "/Users/nataliaresende/Dropbox/PYTHON/join_files.py", line 
14, in join_texts
    with open (file) as x:
FileNotFoundError: [Errno 2] No such file or directory: 
'Chapter23.txt'

import sys
import os
from pathlib import Path

def join_texts():

    files_list=[]

    files_directory = Path(input('Enter the path of the files: '))

    for file in os.listdir(files_directory):
        for f in file:
            with open (file) as x:
                y=x.read()
                files_list.append(y)
    a=' '.join(files_list)
    print(a)

join_texts()

我需要创建一个包含所有这些.txt文件内容的最终文件。有人能帮我编码吗

我喜欢提示用户打开目录,而不是键入目录名

import os
from PySide import QtGui, QtCore

app = QtGui.QApplication(sys.argv)
first_file = unicode(QtGui.QFileDialog.getOpenFileName()[0])
app.quit()
pathname = first_fname[:(first_fname.rfind('/') + 1)]
file_list = [f for f in os.listdir(pathname) if f.lower().endswith('.txt')]
file_list.sort() #you will need to be careful here - this will do alphabetically so you might need to change chapter1.txt to chapter01.txt etc 

这应该可以解决“我需要列表中的文件”问题,但不能将文件合并为一个问题

如果要连接chapter1.txt、chapter2.txt、chapter3.txt,请使用以下代码。。。依此类推,直到第35.txt章:

测试:

您可以使用shutil.copyfileobj:


这是假设您需要指定目录中的所有文本文件;如果没有,请更改if条件

正确的路径应该是:打开os.path,joinfiles\u目录,文件为x:谢谢!问题是这些文件没有按顺序连接。输出文件的第一章是第12章,而不是第01章。有可能解决这个问题吗?你是说文件没有排序?当程序将所有文本放入一个文件时,第一个文本不是文本编号01,而是文本编号12…输出文件应该按照从文件1到文件35的顺序连接所有文本…你能共享文件名吗?文件名是:chapter1.txt,chapter2.txt,在第35.txt章之前,请回溯最后一次调用:File/Users/nataliaresende/Dropbox/PYTHON/join_files2.py,第44行,在join3 File/Users/nataliaresende/Dropbox/PYTHON/join_files2.py,第38行,在join3中使用openitem,“r”作为当前项:FileNotFoundError:[Errno 2]没有这样的文件或目录:“Item016.txt”PyQt4或PyQt5也可以工作,但我认为如果使用这些文件或目录,则不需要[0],因为返回值只是文件名。
import os

def joint_texts():

    files_directory = input('Enter the path of the files: ')
    result = []
    for chapter in range(35):
        file = os.path.join(files_directory, 'chapter{}.txt'.format(chapter+1))
        with open(file, 'r') as f:
            result.append(f.read())
    print(' '.join(result))

joint_texts()
Enter the path of the files: /Users/nataliaresende/Dropbox/XXX
File1Content File2Content File3Content ... File35Content
import os
import shutil

files = [f for f in os.listdir('path/to/files') if '.txt' in f]

with open('output.txt', 'wb') as output:
    for item in files:
        with open(item, 'rb') as current:
            shutil.copyfileobj(current, output)
            output.write(b'\n')