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.
It didn't work using just f = open('file' ,'r+'')
so I tried using the full path and it didn't work either
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 __)
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. Change that string to a formatted one, eg "/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. 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.
➜ 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).
These statements list out the files and read 1 by 1 and print the read content to the console.
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).
>>> 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.
Update
You can try this (with .txt files here):
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.