I am reading [Python Crash Course] and learning how to open a file. I came across this code and wondering of what type “filename” is. The code below suggests it is a list of lines because for loop iterates through it. But when I use ‘type(f)’, some strange output is produced.
<_io.TextIOWrapper name=‘learning_python.txt’ mode=‘r’ encoding=‘cp949’>
I don’t know what that is, and how a for loop could iterate through it even when it is not a list.
filename = 'pi_digits.txt' with open(filename) as f: for line in f: print(line)
You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility
When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!
If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer!