我有一个 Python 脚本可以将文件打印到 shell:
print open(lPath).read()
如果我将路径传递给具有以下内容的文件(没有括号,它们就在这里,所以换行符可见):
> One
> Two
>
我得到以下输出:
> One
> Two
>
>
额外的换行符从何而来?我在 Ubuntu 系统上使用 bash 运行脚本。
请您参考如下方法:
使用
print open(lPath).read(), # notice the comma at the end.
print 添加换行符。如果您以逗号结束 print 语句,它会添加一个空格。
你可以使用
import sys
sys.stdout.write(open(lPath).read())
如果您不需要print 的任何特殊功能。
如果你切换到 Python 3,或者在 Python 2.6+ 上使用 from __future__ import print_function,你可以使用 end 参数来停止 print 添加换行符的功能。
print(open(lPath).read(), end='')
