0

I am reading files in a folder in a python. I want print the each file content separate by a single empty line.

So, after the for loop I am adding print("\n") which adding two empty lines of each file content. How can I resolve this problem?

dimo414
  • 47,227
  • 18
  • 148
  • 244
akira
  • 521
  • 2
  • 5
  • 14

5 Answers5

2
print()

will print a single new line in Python 3 (no parens needed in Python 2).

The docs for print() describe this behavior (notice the end parameter), and this question discusses disabling it.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
1

Because print automatically adds a new line, you don't have to do that manually, just call it with an empty string:

print("")
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

From help(print) (I think you're using Python 3):

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:

file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

So print()'s default end argument is \n. That means you don't need add a \n like print('\n'). This will print two newlines, just use print().

By the way, if you're using Python 2, use print.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
0

print has a \n embedded in it....so you don't need to add \n by yourself

labheshr
  • 2,858
  • 5
  • 23
  • 34
0

Or, if you want to be really explicit, use

sys.stdout.write('\n')

Write method doesn't append line break by default. It's probably a bit more intuitive than an empty print.

Synedraacus
  • 975
  • 1
  • 8
  • 21