I am not very sure where I have gone wrong.
I am trying to print the contents from the page in files.
Here is the code
File>>> If you fell down yesterday, stand up today
text_file = open("files.txt", "r")
print(text_file)
I am not very sure where I have gone wrong.
I am trying to print the contents from the page in files.
Here is the code
File>>> If you fell down yesterday, stand up today
text_file = open("files.txt", "r")
print(text_file)
Take a look at the file objects documentation. An example of iterating over the file includes:
for line in text_file:
print(line)
Or use text_file.readlines()
to receive a list of lines:
['If you fell down yesterday, stand up today']
The way this works in Python is that it takes the file and stores it as just a file in that variable. Thus, you're trying to print a literal file, not the contents. There are a few ways to read things, assuming fvar as the file object for a text doc.
fulltext = fvar.read()
That will put the full text as a string.
oneline = fvar.readline()
Puts one line as the string oneline. Best used in loops. It will read the next line each time it runs - when it hits the end, it will return an empty string.
linelist = fvar.readlines()
This takes every line, as read with readline, and puts it into a list. Performs the same as a loop that reads each line with readline and appends each line to a list.
Hope that helps!