如何使用Python读取文件夹中的多个文本文件? [英] How to read multiple text files in a folder with Python?
问题描述
我看过多个问题&跨SO以及与读取文件夹中的文本文件有关的其他平台的答案,但是不幸的是,目前看来,没有任何一个对我有用.我在一个文件夹中有多个文本文件,想全部读取,然后将每个文本文件作为字符串放入新列表 new_list
.
I have looked at multiple questions & answers across SO, as well as other platforms pertaining to reading text files in a folder, but unfortunately none seems to work for me at the moment. I have multiple text files in a folder and would like to read them all, and put each text file as a string into a new list new_list
.
path = "MyNews_AccidentDataset/News_txt.txt"
all_files = os.listdir(path)
使用此命令可以给我 all_files
作为包含所有文本文件名称的列表
Using this gives me all_files
as a list with names of all text files
'0185_Man dies after 100ft turbine fall .txt',
'0131_Deaths_from_Working_with_Wind_Energy - Copy (5) - Copy.txt',
'0001_BENDING_WITH_THE_WIND._Modern_Power_System_N.txt']
.......
但是,当我使用 open()
读取文件时,
However, when I use open()
to read the file,
new_list = []
for fle in all_files:
# open the file and then call .read() to get the text
with open(fle) as f:
text = f.read()
new_list.append(text)
我收到以下错误:-
with open(fle) as f:
FileNotFoundError: [Errno 2] No such file or directory: '0106_Car_vehicles_part_falls_on_the_roadway.txt'
尽管提到的文件存在于文件夹中.
although the mentioned file exists in the folder.
在这方面的任何帮助都将受到赞赏.
Any help in this regard is appreciated.
编辑使用完整的路径,如@bexi的建议注释
EDIT Using complete path as in suggested comment by @bexi
for fle in all_files:
# open the file and then call .read() to get the text
with open(os.path.join(path, fle)) as f:
text = f.read()
推荐答案
基于其他注释和答案,我得到了 UnicodeDecodeError:'ascii'编解码器无法解码位置643的字节0x93:序数不在范围内(128)
.最后,我可以通过将读取模式设置为二进制"rb"
而不是"r"
:-
Based on some other comments and answers I got UnicodeDecodeError: 'ascii' codec can't decode byte 0x93 in position 643: ordinal not in range(128)
. Finally, I could successfully solve the issue by setting the read mode as binary "rb"
instead of "r"
:-
for fle in all_files:
# open the file and then call .read() to get the text
with open(os.path.join(path, fle),"rb") as f:
text = f.read()
new_list.append(text)
这篇关于如何使用Python读取文件夹中的多个文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!