为什么print语句在python中产生没有任何标签的换行符?

时间:2016-06-02 18:38:37

标签: python python-2.7

这是代码 -

import os

file = open("list.txt", "rw+")
text = file.readline()
print "ffmpeg -loop 1 -i image.jpg -i \""+text+"\" -vf scale=1280:720 -shortest -acodec copy -vcodec mpeg4 \""+text+".mp4\""

os.system("ffmpeg -loop 1 -i image.jpg -i \""+text+"\" -vf scale=1280:720 -shortest -acodec copy -vcodec mpeg4 \""+text+".mp4\"")

但输出是这样的:

ffmpeg -loop 1 -i image.jpg -i "some-file-path
" -vf scale=1280:720 -shortest -acodec copy -vcodec mpeg4 "some-file-path"

它应该在一行

并且ffmpeg会抛出一个文件不存在的错误!

2 个答案:

答案 0 :(得分:5)

因为您的文件在行尾有一个换行符('\n'),所以 所以text"some-file-path\n"

您应该将text = file.readline()更改为text = file.readline().strip()

答案 1 :(得分:-2)

文本文件中的ALl行(当python至少读取它们时)以“\ n”结尾

简易解决方案

text = file.readline()[:-1]

这将修剪该行的最后一个字符“\ n”。 这样做时要小心,因为如果你想要回写一行,你需要在它上面添加一个“\ n”(当使用file.write(“something”)时)