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
:tofor file in filenamesshould solve your issue.fileas a variable name, as it is a reserved word. Better usefile_or something like that in such occasions.