Xonshiz
Xonshiz

Reputation: 1367

passing variables in cmd using python

I want to pass some variable in command prompt with some other text as well. I tried with this code and it doesn't work. Any hints what I might be doing wrong or what should I do instead?

There is a variable "v" which stores a URL and I want to pass this URL in the cmd with some other parameters.I have this code right now.

working_directory = os.getcwd()
p = subprocess.Popen(['ffmpeg -i 'v' -c copy getit.mkv'], cwd=working_directory)
p.wait()

But, it seems like this isn't working. I can't pass the variable "V". It just pastes V when I remove the quotes

Upvotes: 3

Views: 951

Answers (2)

mhawke
mhawke

Reputation: 87074

Pass the command as a list like below:

working_directory = os.getcwd()
p = subprocess.Popen(['ffmpeg', '-i', v, '-c' 'copy' 'getit.mkv'], cwd=working_directory)
p.wait()

Or use shlex.split() which should handle this properly:

cmd = 'ffmpeg -i "{}" -c copy getit.mkv'.format(v)
p = subprocess.Popen(shlex.split(cmd), cwd=working_directory)

Upvotes: 2

number5
number5

Reputation: 16443

To pass a variable into a string you can use format method of string:

v = 'file.avi'
p = subprocess.Popen(["ffmpeg -i {} -c copy getit.mkv".format(v)], cwd=working_directory)

Upvotes: 2

Related Questions