Python not printing

Hi
I’m trying make a code where it prints the last lines of a file, and when i run the code, it runs fine but no output shows. I’m using the readline() command. Here is the code:
with open(“MergeFiles.txt”) as File1:

print("How many lines do you have?")

File1Len = len(File1.readlines())

print("File 1 has: "+str(File1Len)+" Lines")

print("File 1's last line is: "+File1.read(File1Len))

File1.flush()

File1.close()

with open ("MergeFiles2.txt") as File2:

    File2Len = len(File2.readlines())

    print("File 2 has: "+str(File2Len)+" Lines")

    print("File 2's last line is: "+File2.read(File2Len))

    File1.flush()

    File2.close()

and here are the files:
File1 (aka MergeFiles):
Hello : 1
World : 2
This : 3
Is : 4
A : 5
Sentence : 6
bsdgskjnjsngkjs : 7

File2 (aka MergeFiles2):
Hi : 1
I : 2
Am : 3
A : 4
Sentence : 5

Please help.
Thanks!

If you open a file in a context manager (a with section), you don’t need to close it. It’s closed when you leave the section.

If you’re only reading from a file, you don’t need to flush() (there’s nothing for you to flush).

Files can only be read once unless you reopen them or unless you use seek() to change the read pointer. You’re trying to read the file twice. You’re using len(File1.readlines()) to count the length, but that consumes all the data. Issuing a read() after that will return nothing. It looks like you’re expecting the read to return the last line.

If the files are short, read them into a structure (a str or list depending on what you need) and count the lines at that point. Then you still have the data you can print or examine without re-reading it from the filesystem.

Hi Hrishikesh, and welcome!

You said:

I’m trying make a code where it prints the last lines of a file, and
when i run the code, it runs fine but no output shows.

That is not true. The code does not run fine, it raises an exception,
and there is output. When I run your code, it does this:

[steve test]$ python3 test.py 
How many lines do you have?
File 1 has: 7 Lines
File 1's last line is: 
File 2 has: 5 Lines
File 2's last line is: 
Traceback (most recent call last):
  File "test.py", line 12, in <module>
    File1.flush()
ValueError: I/O operation on closed file.

So the first thing to do is to fix the failing line. You are calling
flush on a file which is already closed. Since you are only reading
files, you don’t need to call flush at all. It is a waste of time, there
is nothing to flush.

Same with the calls to close: you are using the with open(...) context
manager, which automatically calls close. So manually closing the files
is a waste of time too. So let us fix those two problems:

with open("MergeFiles.txt") as File1:
    print("How many lines do you have?")
    File1Len = len(File1.readlines())
    print("File 1 has: "+str(File1Len)+" Lines")
    print("File 1's last line is: "+File1.read(File1Len))
    with open ("MergeFiles2.txt") as File2:
        File2Len = len(File2.readlines())
        print("File 2 has: "+str(File2Len)+" Lines")
        print("File 2's last line is: "+File2.read(File2Len))

Now when we run it again, at least there is no exception, and the output
becomes:

How many lines do you have?
File 1 has: 7 Lines
File 1's last line is: 
File 2 has: 5 Lines
File 2's last line is: 

Okay, so now we can look more closely at the output without the
exception. Your code is correctly getting the length of the file, but it
is printing the empty string instead of the last line. Why?

Let us look at the lines that print the empty string for the last line:

print("File 1's last line is: "+File1.read(File1Len))

There is the problem. You have already read the entire file using
readlines, so when you go to read another 7 characters, there is nothing
more to read and you get an empty string. Let’s fix that:

with open("MergeFiles.txt") as File1:
    print("How many lines do you have?")
    lines1 = File1.readlines()
    File1Len = len(lines1)
    print("File 1 has: "+str(File1Len)+" Lines")
    print("File 1's last line is: " + lines1[File1Len-1])
    with open ("MergeFiles2.txt") as File2:
        lines2 = File2.readlines()
        File2Len = len(lines2)
        print("File 2 has: "+str(File2Len)+" Lines")
        print("File 2's last line is: " + lines2[File2Len-1])

and now the output becomes:

How many lines do you have?
File 1 has: 7 Lines
File 1's last line is: bsdgskjnjsngkjs : 7

File 2 has: 5 Lines
File 2's last line is: Sentence : 5

Thanks a lot!