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?
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?
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.
Because print automatically adds a new line, you don't have to do that manually, just call it with an empty string:
print("")
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.
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.