Why is len(file.read()) giving me a value of zero?

2024/11/8 2:36:53

Why are the values of print len() different for both functions? Are they not the same?

The file this script is opening was a text file with three lines of text. i named it test.txt and inside it was

Jack and Jill
gave up 
they went home with no water

Code:

def function2nd (filename):target = open(theFile, 'r')inData = target.read()print inDataprint len(inData)target.close()
theFile = raw_input("What is the file name?\n>>")
function2nd(theFile)def function3rd (filename):target = open(theFile, 'r')target.read()print target.read()print len(target.read())target.close()function3rd(theFile)
Answer

Files act like a long tape in a casette; you can read the file but by the time you are done you have passed the tape all the way to the end. Reading again won't give you the data again.

As such your second function tried to read data from a file that is already wound all the way to the end.

You can rewind the 'tape' by re-opening the file, or by using target.seek(0) to send it back to the start.

https://en.xdnf.cn/q/120671.html

Related Q&A

Re-formatting user input with spaces

Im using an input function where I want to convert any spaces in the input to +s. So for example, if the user inputs iphone 7 black, I want to convert this to iphone+7+black.

How to set a pdf background color? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

Row Average CSV Python

Im looking for a piece of code that will print the average for each users score from a csv.It needs to read all scores and then work out an average across the row for each users.It also needs to calcul…

To input a file and get a formatted string output using .format()

Write a function named calculate_expenses that receives a filename as argument. The file contains the information about a persons expenses on items. Your function should return a list of tuples sorted …

How to use python regex to extract IP address from server log files?

I am currently getting started with python. I have a server log file for the pages I visited over a period of time. How do I write a python program to find out which IP address was visited most? Will …

DateFormatter returning wrong dates - Matplotlib/Pandas [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions concerning problems with code youve written must describe the specific problem — and incl…

What does the error IndentationError: expected an indented block in python mean? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

How to insert character in csv cell in python?

Im new with python. Here is my csv file :data;data;name surname; data; data data;data;name surname; data; data data;data;name surname; data; data data;data;name surname; data; dataThe thing that I want…

How can you initialise an instance in a Kivy screen widget

I am trying to access an instance variable named self.localId in my kivy screen and it keeps saying the saying the instance doesnt exist after i have initialised it. I know I have an error In my code b…

is a mathematical operator classed as an interger in python

in python is a mathematical operator classed as an interger. for example why isnt this code workingimport randomscore = 0 randomnumberforq = (random.randint(1,10)) randomoperator = (random.randint(0,2)…