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
  • 44,897
  • 17
  • 143
  • 228
akira
  • 473
  • 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
  • 44,897
  • 17
  • 143
  • 228
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
  • 115,525
  • 42
  • 225
  • 281
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
  • 20,142
  • 17
  • 60
  • 81
0

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

labheshr
  • 2,568
  • 2
  • 20
  • 30
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
  • 895
  • 7
  • 19