python - Why subprocess.call works with any string only if parameters are separated in an array? -
python - Why subprocess.call works with any string only if parameters are separated in an array? -
if prepare command line this:
cmd = "ffmpeg -i '" + param1 + "' -o '" + param2 + "'
and seek utilize subprocess.call or os.system may work or not depending of characters param1 or param2 contain. when fails exception raised similar this: unicodedecodeerror 'ascii' codec can't decode byte 0xxx
. if this:
subprocess.call(["ffmpeg", "-i", param1, "-o", param2], ..., ...)
it works always.
now i'm using sec method works want understand why first method doesn't work.
things tried:
using decode() , decode("utf8") in param1 , param2. using cmd.encode("ascii") when passing cmd os.system or subprocess.callunset lang
in terminal prior run script (somebody solved similar problem doing that, tried too) more info:
this started when needed query mysql database path directory. table uses utf-8. @ first did not specify encoding when establishing connection database, results in paths not found when contain non ascii characters. illustration word "vídeos" decoded "v\xxxeos" \xxx
code of "í" character. when started specify encoding when connecting database non english language words looked correctly when printed unicodedecodeerror started raise when trying utilize strings in calls os.system or subprocess.call if command line build concatenating strings.
python 2.7 mythbuntu 32 bits
interpreting command string (into argc
, argv
, i.e. dynamically sized array of strings, expected c programs) job of shell, , different shells have different rules how interpret particular string. subprocess.call
not invoke subprocesses in new subshell default; instead, creates new process directly, , hence not know how interpret command, must pass array directly.
you can create subprocess.call
execute in subshell passing shell=true
:
subprocess.call(cmd, shell=true)
regarding encodings, yes, responsibility of programme beingness called interpret string encoding of arguments. if you've set utf-8 everywhere, should work:
>>> import subprocess >>> subprocess.call("echo 字", shell=true) 字 0 >>> subprocess.call(u"echo 字", shell=true) 字 0 >>> subprocess.call(["echo", "字"]) 字 0 >>> subprocess.call([u"echo", u"字"]) 字 0
python mysql python-2.7 encoding utf-8
Comments
Post a Comment