Python Forum
(Beginners problem) Does file change during this code? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: (Beginners problem) Does file change during this code? (/thread-35448.html)



(Beginners problem) Does file change during this code? - fiqu - Nov-03-2021

I dont understand what makes the two "print('# items are:', len(code))" give different results? Does the code change anything in the cfile? And why do i get the result "# of lines: 0" when i know they are 1909 in the file.
[attachment=1365]


RE: (Beginners problem) Does file change during this code? - bowlofred - Nov-03-2021

Please don't use images of code. Paste it as text between python tags (use the python button in the editor). Here's your code:

cfile = open('/content/drive/MyDrive/Colab_Notebooks/py4e_7.1.txt')
nlines=0
code=0
code=cfile.read()
print('# items are:', len(code))
for line in cfile:
  nlines=nlines+1
code=cfile.read()
print('# of lines:', nlines)
print('# items are:', len(code))
On line 4, you use read(). This reads the entire file into code (as a string of text). The length of a string is the number of characters in it, so when you print # items, it's printing the number of characters in the file.

In the for loop on line 6, assigning from the open file will read in a line at a time. But the previous read() has consumed everything. There are no lines remaining.

One line 8, you again try to read the entire file into code. But since the file has been read, it's at EOF and this just returns the empty string.

Since the loop didn't run, nlines is 0 and since you overwrite code with an empty string, len(code) is zero.

Get rid of the two cfiile.read() lines and the loop should at least run and give you the number of lines in the file.


RE: (Beginners problem) Does file change during this code? - fiqu - Nov-03-2021

Ok, thanks @bowlofred
How would you write it to get both number of characters and number of lines printed out at the same time?


RE: (Beginners problem) Does file change during this code? - bowlofred - Nov-03-2021

(Nov-03-2021, 10:03 PM)fiqu Wrote: Ok, thanks @bowlofred
How would you write it to get both number of characters and number of lines printed out at the same time?

How important is getting the character length? Often it's easier to just read the data line-by-line. But that mechanism lets python split (and discard) the newline characters. Different formats might have different newlines. Doing it that way wouldn't be able to tell between a unix-style <LF> and a windows style <CR><LF>.

So my first thought is to not try. But if you really had to, you have a couple of ways.
* read in the entire file (count the characters), then split it into lines. Fine for "regular" files
* read in the file in chunks (count the characters), then re-read it into lines. May be necessary for "huge" files to avoid memory issues.


with open('/content/drive/MyDrive/Colab_Notebooks/py4e_7.1.txt') as cfile:
    entire_file = cfile.read()
print(f"The file has {len(entire_file)} characters")

for line in entire_file.splitlines():
    total_lines = 0
    for line in cfile:
        total_lines += 1
print(f"The file has {total_lines} lines")