[英]How to open several text files in a for loop
I have several text files in a list all_files
and I want to make a loop that open each file, get the values inside, close the file and then go to the next. 我在
all_files
列表中有几个文本文件,我想做一个循环来打开每个文件,获取其中的值,关闭文件,然后转到下一个。
It didn't work using just f = open('file' ,'r+'')
so I tried using the full path and it didn't work either 仅使用
f = open('file' ,'r+'')
不能正常工作,所以我尝试使用完整路径,但也无法正常工作
import os
all_files = os.listdir("text_file") #my list of files
for file in all_files:
f = open("/Users/stordd/Desktop/StageI2M/Leiden/text_file/file", 'r+')
I constantly have the error FileNotFoundError: [Errno 2] No such file or directory
and i don't understand how to use the os.path.dirname(os.path.abspath(__file __) 我经常出现错误
FileNotFoundError: [Errno 2] No such file or directory
,我不明白如何使用os.path.dirname(os.path.abspath(__ file __)
Your file string is wrong. 您的文件字符串错误。 It should not be
"/Users/stordd/Desktop/StageI2M/Leiden/text_file/file"
, because it would look for file named file
inside text_file
directory in each iteration. 它不应为
"/Users/stordd/Desktop/StageI2M/Leiden/text_file/file"
,因为它会在每次迭代中在text_file
目录中查找名为file
。 Change that string to a formatted one, eg "/Users/stordd/Desktop/StageI2M/Leiden/text_file/{}".format(file)
. 将该字符串更改为格式化的字符串,例如
"/Users/stordd/Desktop/StageI2M/Leiden/text_file/{}".format(file)
。 So finally the code should look like: 因此,最终代码应如下所示:
import os
all_files = os.listdir("text_file") #my list of files
for file in all_files:
f = open("/Users/stordd/Desktop/StageI2M/Leiden/text_file/{}".format(file), 'r+')
In my case, I am in file_handling
directory. 就我而言,我在
file_handling
目录中。 The following will help you to figure out. 以下内容将帮助您找出答案。
I did not modify your code and use it (Just tried to make a similar attempt to help you) because I want you to fix by looking at my solution (If you haven't fixed yet).
我没有修改您的代码并使用它(只是尝试了类似的尝试来帮助您),因为我希望您通过查看我的解决方案来进行修复(如果您尚未修复)。
➜ file_handling git:(master) ✗ pwd
/Users/hygull/Projects/Python3/stkovrflw/2019/try/file_handling
Here, I have 3 files as shown below. 在这里,我有3个文件,如下所示。
➜ file_handling git:(master) ✗ ls
A.txt B.txt reader.py
➜ file_handling git:(master) ✗
➜ file_handling git:(master) ✗ cat A.txt
(A) I know Python
➜ file_handling git:(master) ✗ cat B.txt
(B) Data Science is really cool
➜ file_handling git:(master) ✗
Now, below is the Python code executed from /Users/hygull/Projects/Python3/stkovrflw/2019/try/
directory (where file_handling
directory resides). 现在,下面是从
/Users/hygull/Projects/Python3/stkovrflw/2019/try/
目录执行的Python代码( file_handling
目录所在的目录)。
These statements list out the files and read 1 by 1 and print the read content to the console. 这些语句列出文件,并以1的顺序读取,并将读取的内容打印到控制台。
Note: Here you don't need to close file explicitly as we are using with statement which takes care of it (Pyhonic way of reading file).
注意:在这里,您不需要显式地关闭文件,因为我们正在使用with语句来处理该文件(Pyhonic读取文件的方式)。
>>> import os
>>>
>>> all_files = os.listdir('file_handling')
>>>
>>> all_files
['reader.py', 'B.txt', 'A.txt']
>>>
>>>
>>> for file in all_files:
... dir_full_path = os.path.abspath('file_handling') # e.g. /Users/rishi/file_handling
... file_full_path = os.path.join(dir_full_path, file) # e.g. /Users/rishi/file_handling/A.txt
... with open(file_full_path, 'r+') as f:
... content = f.read()
... print(content)
... print('---' * 30, '\n')
...
------------------------------------------------------------------------------------------
(B) Data Science is really cool
------------------------------------------------------------------------------------------
(A) I know Python
------------------------------------------------------------------------------------------
>>>
You could try this: 您可以尝试以下方法:
with open('_name_of_file_', 'r+') as f, open('_name_of_file2_', 'r+') as g:
something()
It works for an old Python version, keep me in touch. 它适用于旧的Python版本,请保持联系。
Update 更新
You can try this (with .txt files here): 您可以尝试此操作(此处带有.txt文件):
import glob
path = /somewhere/you/decide
for file in glob.glob(os.path.join(path, '*.txt')):
with open(filename, 'r') as f:
text = f.read()
print(text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.