0

This program (written in Python) is supposed to display only the first two lines of text written to a particular file. However when I run it, though it has no errors it doesn't display any output, in the IDE nor on the file itself.

def file_read_from_head(fname, nlines):
    from itertools import islice
    with open(fname) as f:
        for line in islice(f, nlines):
            print(line)


f = open('test.txt', 'w')
f.write = ("Hello welcome to Python \n"
           "THis is the second line \n"
           "This is the third line \n")


print(file_read_from_head('test.txt', 2))
jwalton
  • 5,286
  • 1
  • 18
  • 36
  • 3
    That `=` should not be there in `f.write = (...)`. You want to call `f.write`, not assign to it. You should also close `f` after writing (before you try and read what you've written). – khelwood Oct 18 '20 at 10:26
  • What @khelwood said. Also you might need to close the file (`f.close()`) before reading it, because the text is written to a buffer first. Closing the file guarantees that all text from the buffer is written to the actual file on disk. – Niklas Mertsch Oct 18 '20 at 10:29

2 Answers2

2

A few notes:

  1. as indicated in the comments you should be calling f.write(), not assigning output to a variable called f.write
  2. After your call to f.write() you also need to close the file. You can achieve this with f.close(). However, it is much better practice to use a context manager (see the comments below this answer). Using a context manager makes it easier to avoid mistakes (such as forgetting to close a file...). You actually already use a context manager in your file_read_from_head() function.
  3. Your function file_read_from_head() calls print(line), and so print(file_read_from_head()) isn't needed (nor is doing what you intend)
  4. It is generally considered bad practice to import within functions (see this question for a discussion). Instead it preferred to have all your import statements at the top of your file.

Considering all the above, we can rework your code as:

from itertools import islice

def file_read_from_head(fname, nlines):
    with open(fname) as f:
        for line in islice(f, nlines):
            print(line)


# Context managers make it easier to avoid forgetting to close files
with open('test.txt', 'w') as f:
    f.write("Hello welcome to Python \n"
            "This is the second line \n"
            "This is the third line \n")


file_read_from_head('test.txt', 2)
jwalton
  • 5,286
  • 1
  • 18
  • 36
  • 1
    rather than suggesting to `close` the file, you should really be suggesting to use `with` instead – Tomerikoo Oct 18 '20 at 10:34
  • There's nothing *wrong* with opening and closing a file as OP did. But I agree, using a context manager is much cleaner (also I hinted at this in the answer) – jwalton Oct 18 '20 at 10:36
  • There a few things wrong with opening and closing files, one of them is presented above in the question.... I think you edited that in right when I was writing the comment. I would also the code to match that suggestion – Tomerikoo Oct 18 '20 at 10:37
  • Again, *it is not wrong*. Yes, it's easier to make mistakes, and is less pythonic, and I would always advocate using a context manager. but it is not "wrong" – jwalton Oct 18 '20 at 10:38
  • 2
    Yes I guess this argument can go on forever because it depends on how you define wrong... The idiomatic way and the *best* one to handle files is to use `with`. Which your answer suggests to do, which is perfect, I would just change the code sample as-well and avoid using `open/close` in my answers at all – Tomerikoo Oct 18 '20 at 10:40
0

you've done a few things wrong. first f.write is a function not a variable. so replace it with:

f.write("Hello welcome to Python \n"
       "THis is the second line \n"
       "This is the third line \n")
f.close()

when you're opening the file in the file_read_from_head function, you're not providing a read/write flag so replace it with:

with open(fname, 'r') as f:

also you're printing the result of file_read_from_head, which would be none so you should remove the print() around the function call if you want to.

Shahzaib
  • 46
  • 4
  • 2
    Your second suggestion is irrelevant as the default mode for opening files is `'r'`, so in that case `open(fname)` is perfectly fine – Tomerikoo Oct 18 '20 at 10:42