Xplore
Xplore

Reputation: 381

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 !

Upvotes: 2

Views: 60

Answers (2)

Space Bear
Space Bear

Reputation: 814

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

Upvotes: -2

DeepSpace
DeepSpace

Reputation: 81684

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().

Upvotes: 5

Related Questions