0

I need to declare filenames in an array and pass them to open with method in python.File name needs to be sent as parameter from the array. Please let me know if the code below is fine.

filenames= ["abc.txt", "def.txt", "ghi.txt"] 

Code for iterating

for file in filenames

    with open(file,'r')

My expectation is to iterate through the filenames and open them like

with open('abc.txt', 'r') #for first run

with open('def.txt', 'r') #for second run

with open('ghi.txt', 'r') #for third run
5
  • Open them in a sequence, that is it? Commented Feb 21, 2019 at 6:30
  • I was going to edit your question but then it becomes answer :P. Adding : to for file in filenames should solve your issue. Commented Feb 21, 2019 at 6:31
  • You should not use file as a variable name, as it is a reserved word. Better use file_ or something like that in such occasions. Commented Feb 21, 2019 at 7:43
  • Thanks Chris for editing the question and correcting it. Commented Feb 24, 2019 at 11:31
  • @SSRSeln were you able to solve this? You may accept the answer that helped you: meta.stackexchange.com/questions/5234/… Commented Mar 5, 2019 at 6:59

3 Answers 3

2
filenames= ["abc.txt", "def.txt", "ghi.txt"]

for i in range(len(filenames)):
    with open(filenames[i], 'r') as fileObj:
        # do the rest

Or just:

for file in filenames:
    with open(file, 'r') as fileObj:
        fileObj.readlines()
        # do the rest
Sign up to request clarification or add additional context in comments.

Comments

2

You can try:

filenames= ["abc.txt", "def.txt", "ghi.txt"] 
for single_filename in filenames:
    with open(single_filename, 'r') as file_object:
        print file_object.readlines()

1 Comment

and what if I want something similar but my filenames is [1,2,3,4,5,6,7] (numbers) and with the same for loop I want to read 1.txt, 2.txt and so on?
-1

If your files are in the same location as the python program, the below snippet is a good reference.

filenames= ["abc.txt", "def.txt", "ghi.txt"]
for i in filenames:
    with open(i,"r"):
        #do something with files here

In case your files are located somewhere else, try providing the full path in the list. Or use a prefix like below:

filenames= ["abc.txt", "def.txt", "ghi.txt"]
path_prefix = <path here>
for i in filenames:
    with open(path_prefix + i,"r"):
        #do something with files here

3 Comments

Your second solution doesn't use path_suffix. Besides, always better to use os.path.join than simply adding strs.
i missed 'r' in the open block, and somehow edit is not being passed. Just make sure to add that if you use it.
read comment above. Also , i is not the iteration number, it is the file name as I have not used range(). Just because I called it i and not files, does not mean it is an integer :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.