I’m trying to call ‘sed’ from Python and having troubles passing the command line via either subprocess.check_call() or os.system().
I’m on Windows 7, but using the ‘sed’ from Cygwin (it’s in the path).
If I do this from the Cygwin shell, it works fine:
$ sed 's/&nbsp;/\ /g' <"C:foobar" >"C:foobar.temp"
In Python, I’ve got the full pathname I’m working with in “name”. I tried:
command = r"sed 's/&nbsp;/\ /g' " + "<" '\"' + name + '\" >' '\"' + name + '.temp' + '\"'
subprocess.check_call(command, shell=True)
All the concatenation is there to make sure I have double quotes around the input and output filenames (in case there are blank spaces in the Windows file path).
I also tried it replacing the last line with:
os.system(command)
Either way, I get this error:
sed: -e expression #1, char 2: unterminated `s' command
'amp' is not recognized as an internal or external command,
operable program or batch file.
'nbsp' is not recognized as an internal or external command,
operable program or batch file.
Yet, as I said, it works OK from the console. What am I doing wrong?
I agree with Ned Batchelder’s assessment, but think what you might want to consider using the following code because it likely does what you ultimately want to accomplish which can be done easily with the help of Python’s
fileinputmodule:This will effectively update the given file in place as use of the keyword suggests. There’s also an optional
backup=keyword — not used above — which will save a copy of the original file if desired.BTW, a word of caution about using something like
C:foobarto specify the file name because on Windows it means a file of that name in whatever the current directory is on drive C:, which might not be what you want.