简体   繁体   中英

Why print statement produces a newline without any tags in python?

Here's the code -

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\"")

But the output is like this :

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

Whereas it should be in a single line

And ffmpeg throws an error that file doesn't exist !

Because your file has a new line character ( '\\n' ) in the end of the line,
so text is "some-file-path\\n" .

You should change text = file.readline() to text = file.readline().strip() .

ALl lines in textfiles (when python reads them at least) end in "\\n"

easy solution

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

this will trim the last character "\\n" from the line. careful when doing this, since if you want to write the line back, you need to append a "\\n" to it (when useing file.write("something"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM