0

In Python, the syntax

for line in f:
    sys.stdout.write(line)

can be used to print a file. Except it does not work when the file's last line is incomplete, does not end in a newline. What to do then?

> echo -n > foobar.txt
> cat foobar.py
#!/usr/bin/python

import sys

for line in open("foobar.txt"):
    sys.stdout.write(line)
> ./foobar.py
>
Mark Galeck
  • 6,155
  • 1
  • 28
  • 55
  • 4
    Why wouldn't that work if the last line is incomplete? – mgilson Sep 15 '14 at 22:22
  • 1
    What exactly do you mean *"doesn't work"*?! – jonrsharpe Sep 15 '14 at 22:24
  • The last line is not printed – Mark Galeck Sep 15 '14 at 22:25
  • What makes you think the last line isn't printed? – user2357112 Sep 15 '14 at 22:33
  • @user2357112 I show you above in the edited question – Mark Galeck Sep 15 '14 at 22:34
  • 1
    You're creating an empty file. There is nothing to print. Why do you think the nothing isn't getting printed properly? – user2357112 Sep 15 '14 at 22:35
  • 1
    Sorry everybody I got confused. ID10T error code. God please just let the earth open beneath me so I may fall in. – Mark Galeck Sep 15 '14 at 22:37
  • OK everybody, sorry again, THE REASON, why this was the question, is because, in a more complex example that does not work :) I will post that as a separate question in a moment . I basically thought "I will simplify it this way" - the simplified example does work, I got autosuggested. – Mark Galeck Sep 15 '14 at 22:43
  • @MarkGaleck: Flush the output. `sys.stdout.flush()`. Otherwise, the output will only come out at a point that depends on the buffer mode; when printing to a terminal, that generally means when you write a newline, and you're not writing a newline. Depending on the situation, you may also want to add a newline if the file doesn't end with one. – user2357112 Sep 15 '14 at 22:53

2 Answers2

3

You can check if the line contains a newline character.

for line in f:
    if "\n" in line:
        sys.stdout.write(line)
    else:
        sys.stdout.wrote(line + "\n")

I did some more research on sys.stdout.write() and it appears that you're going to need to force it to add a newline character to the end of strings that do not contain them. From what I've read (Python - The difference between sys.stdout.write and print) print automatically does this for you then pushes the remaining string into sys.stdout.write. It might appear like a line in the file isn't being written properly if there's no newline character because it will be written to the same line on the console as the previous file's line. However, it should still be writing somewhere.

Community
  • 1
  • 1
asdf
  • 2,927
  • 2
  • 21
  • 42
  • Yes but like I said in the question I want to print the file. How to print the incomplete line. – Mark Galeck Sep 15 '14 at 22:25
  • @MarkGaleck Even if the line does not contain a newline character it should still print. Maybe you're experiencing some sort of data corruption? Could you post the error you're seeing in your question? Or maybe some sample I/O for us to read? – asdf Sep 15 '14 at 22:26
  • I posted the example for you – Mark Galeck Sep 15 '14 at 22:33
1

You can check to see if the line ends with a newline:

for line in f:
    if line.endswith('\n'):
        # complete line
    else:
        # incomplete line

If you just want to print the line, you can just strip the line first:

for line in f:
    # Remove any trailing newline; `print` will add one automatically.
    print line.rstrip('\n')
squiguy
  • 32,370
  • 6
  • 56
  • 63
nneonneo
  • 171,345
  • 36
  • 312
  • 383