[英]bash: -c: line 0: syntax error near unexpected token `newline' when running a linux cmd from inside python-os.system
[英]Running python in subprocess.call(cmd, ...) get error with message /bin/sh: -c: line 0: syntax error near unexpected token `('
我的代碼(python3 在virtualenv
運行)
try:
cmd = "cd NST/experiments && python main.py eval --model models/21styles.model --content-image " + directory + "/" + filename + " --style-image " + STYLE_IMAGE_UPLOAD + "wikiart/" + choose_file() + " --output-image " + OUTPUT_IMAGE_UPLOAD + filename + " --content-size 600" " --cuda=0"
returned_value = subprocess.call(cmd, shell=True) # returns the exit code in unix
print('returned value:', returned_value)
except Exception as e:
print(e)
我在運行腳本時遇到了這個錯誤
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `cd NST/experiments && python main.py eval --model models/21styles.model --content-image /Users/kanel/Documents/Developments/ML/applications/static/nst/content_image/en_20191208/20.jpg --style-image /Users/kanel/Documents/Developments/ML/applications/static/nst/style_image/wikiart/facsimile-of-may-courtly-figures-on-horseback(1).jpg --output-image /Users/kanel/Documents/Developments/ML/applications/static/nst/output_image/20.jpg --content-size 600 --cuda=0'
您在命令行中有未加引號的字符串。 通常,默認情況下引用系統調用(或子進程等)的任何傳遞值/變量是很好的,否則可能會像現在這樣出現錯誤。
我對你的源代碼進行了一些重組,添加了引用,並為源代碼塊增加了更多的可讀性。
這是應該適合您的代碼:)
try:
from pipes import quote as quote_path
except ImportError:
from shlex import quote as quote_path
try:
cmd = (
"cd NST/experiments && python main.py eval --model models/21styles.model "
"--content-image {} "
"--style-image {} "
"--output-image {} "
"--content-size 600 --cuda=0"
).format(
quote_path(directory + " / " + filename),
quote_path(STYLE_IMAGE_UPLOAD + " wikiart / " + choose_file()),
quote_path(OUTPUT_IMAGE_UPLOAD + filename)
)
returned_value = subprocess.call(cmd, shell=True) # returns the exit code in unix
print('returned value:', returned_value)
except Exception as e:
print(e)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.