
Kellen A.
asked 12/23/22Computer Science. Why doesn't this work?)
f = open("data.txt")
a = 0
for line in f:
a = a + len(line)
print(a)
2 Answers By Expert Tutors
William M. answered 01/07/23
Computer/HS Instructor w/7+ years teaching Computer Science
First, your code should either be in functions, or in classes, not at global scope
Second, you have not shown an example of the type of data in data.txt
Are there multiple data values per line?
Are they all ints, or all doubles, or a mixture of each?
Are there a variable number of values per line?
For example
is data.txt like this...
10 20 30 40 50
60 70
80 90 100
...
or is data.txt like this...
10 25.5 30 40.7
50.1 70
... ?
Third, you do not close f after you are done using it,
but you can ensure this is always done by writing in a Pythonic way (what an experienced Python developer would write)
with open('data.txt') as f: # is the correct syntax -- it automatically closes f at the end of the with block
... more code here
Send the example, and we can answer your question completely

Michael F. answered 12/30/22
30 years college professor of undergraduate math and computer science
It's reasonable to hope that the output of your code would be the size of the data.txt file.
There are two things that get in the way:
- As Daniel points out, there is a special character or character pair that marks the end of a line in a text file: Ascii code 13 (ctrl-M) is the carriage return character, and Ascii 10 (crtl-J) is the line feed character. Windows and DOS use both of them, and Unix/Linux use only the line feed. The Python readline() function when running it Windows will (or at least should) turn the 2-character end-of-line code into the single character called '\n' . That is Python's one-character end-of-line marker regardless of which operating system is being used.
- Many text files any more are UTF-16 Unicode, where each character is encoded with 2 bytes. Java uses that, for example. File sizes of text files are in bytes, not in number of characters. UTF-16 text files also start with a two-byte code indicating which of the two bytes of a character are less significant So is the 16-bit code 65 (for 'A') written as byte pair (65, 0) (little endian) or as byte pair (0, 65) (big endian)? Most processors default to little endian any more (since Apple went to Intel processors), but that code is required in a UTF-16 text file.
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Daniel B.
12/23/22