Question

I'll start by saying that I am very, very new to Python.

I used to have a Windows/Dos batch file in order to launch Mencoder with the right set of parameters, without having to type them each time.

Things got messy when I tried to improve my script, and I decided that it would be a good opportunity to try coding something in python.

I've come up with that :

#!/usr/bin/python
import sys, os

#Path to mencoder
mencoder = "C:\Program Files\MPlayer-1.0rc2\mencoder.exe"
infile = "holidays.avi"
outfile = "holidays (part1).avi"
startTime = "00:48:00"
length = "00:00:15"

commande = "%s %s -ovc copy -oac copy -ss %s -endpos %s -o %s"
os.system(commande % (mencoder, infile, startTime, length, outfile))

#Pause
raw_input() 

But that doesn't work, windows complains that "C:\Program" is not recognized command.

I've trying putting some "\"" here and there, but that didn't help.

Was it helpful?

Solution

Python have two types of quotes, " and ' and they are completely equal. So easiest way to get quotes in a string is to say '"C:\Program Files\MPlayer-1.0rc2\mencoder.exe"'.

Using the raw prefix (ie r'"C:\Program Files\MPlayer-1.0rc2\mencoder.exe"') is a good idea, but that is not the error here, as none of the backslashes are followed by a letter that is an escape code. So your original string would not change at all by having an r in front of it.

OTHER TIPS

use two quotes instead of one if you are doing on windows.

"\\"

I'm new to Python but I know when ever I see that problem, to fix it, the file (executable or argument) must be in quotes. Just add \" before and after any file that contains a space in it to differentiate between the command-line arguments. So, that applies to your outfile variable as well. The code should look like this...

#!/usr/bin/python
import sys, os

#Path to mencoder
mencoder = "\"C:\Program Files\MPlayer-1.0rc2\mencoder.exe\""
infile = "holidays.avi"
outfile = "\"holidays (part1).avi\""
startTime = "00:48:00"
length = "00:00:15"

commande = "%s %s -ovc copy -oac copy -ss %s -endpos %s -o %s"
os.system(commande % (mencoder, infile, startTime, length, outfile))

#Pause
raw_input()

You can even put the mencoder.exe into a directory which doesn't have a space char inside it's name (opposed to Program Files).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top