I have a list of file pathnames, and I want to iterate through this list opening each file and extracting certain data points from it. However, when I run the code, it doesn't open the file it just takes the nth character from the file pathname and adds it to the target list, instead of the value from the file that I want.
for filename in file_list:
file_data = open(filename, 'r')
data_one.append(filename[5])
data_two.append(filename[6])
data_three.append(filename[7])
data_four.append(filename[8])
data_five.append(filename[9])
file_data.close()
What have I missed to make it so that it doesn't actually open the files?
CodePudding user response:
You want to read from the file-like object file_data
, not the file name, in order to update each list. Further, file-like objects are iterable, but they cannot be indexed. If you want lines 6-10, you'll want something like itertools.islice
.
from itertools import islice
for file_name in file_list:
with open(file_name) as f:
points = islice(f, 5, 10)
data_one.append(points[0])
data_two.append(points[1])
# etc
I'm probably making too many assumptions about the actual file format, but the main point is: you have to use the file-like object returned by open
instead of the file name.
CodePudding user response:
Assuming that your file looks like:
line0
line1
line2
line3
line4
line5
line6
line7
line8
line9
and you want to read lines "line5" to "line9", this is the correct code:
for filename in file_list:
with open(filename, 'r') as f: # This line opens the file and will close it
data = f.readlines()
data_one.append(data[5]) # These lines use `data` and not the filename
data_two.append(data[6])
data_three.append(data[7])
data_four.append(data[8])
data_five.append(data[9])