1

I have multiple files, let's say data{1..10}.txt in folder data/

I need to read these files into python environment for further use, example code i have in mind is:


chr_list = []

x = 0
for item in range(10):
    datafile="data/data"+str(i+1)+".txt"
    chr_list[x] = open(datafile, "r")
    x += 1

The expected outcome would be

print(chr_list[0])

#prints out content of file data/data1.txt

This code doesn't work even after correcting all possible errors. Thus, i would like to see some examples on how to reach my desired outcome.

4
  • 1
    You must actually read the file content: chr_list.append(open(datafile, "r").read()) Commented Dec 22, 2019 at 13:40
  • The code still brings out the same errors, so i assume there has to be a deeper mistake in a code Commented Dec 22, 2019 at 13:42
  • Sorry, the as the list was originally empty, you must append to it. I updated my comment. Commented Dec 22, 2019 at 13:45
  • Please take care to always include the complete error traceback in your question when you have an error, it makes it easier to spot the problems in your code. Commented Dec 22, 2019 at 13:48

2 Answers 2

3

As Thierry Lathuille pointed out, to get the contents of the file you have to actually call the read method. Without calling this, you will have a list of open files, not their contents. You can also shorten your code by using the for-loop counter instead of incrementing your own value.

New code:

chr_list = []

for x in range(10):
    datafile="data/data"+str(i+1)+".txt"
    chr_list.append(open(datafile).read())

EDIT:

The error comes from the fact that you can't add items to a list by referencing a new index - you have to use the append method.

EDIT 2:

You can also cut down your code by using list comprehensions:

chr_list = [open("data/data"+str(i+1)+".txt").read() for i in range(10)]
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, both answers solved it. How can i mark both as correct?
I don't think you can... Does yganalyst's answer actually produce a list of the contents?
1

It can do with a dictionary not a list.

chr_list = {}

x = 0
for i in range(10):
    datafile="data/data"+str(i+1)+".txt"
    chr_list[x] = open(datafile, "r")
    x += 1
chr_list[0]

Or

chr_list = {}

for i in range(10):
    name = "data" + str(i+1)
    chr_list[name] = open("data/"  + name + ".txt", "r")
chr_list['data1']

5 Comments

Changing [] to {} fixes an issue.
This code won't currently add the contents of the files to the list/dictionary... It will make a list of file objects.
When calling chr_list[0] it gives the content of the file though, what am i missing?
chr_list[0] isn't an index when using dictionaries in this way... use chr_list['data1'] instead... Also, using this code, chr_list['data1'] gives <_io.TextIOWrapper name='data1.txt' mode='r' encoding='cp1252'>, not the content of data1.txt...
You’re not actually reading the contents of the file, nor are you properly handling it using a context manager.

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.