Python菜单脚本通过xterm启动二进制程序报错问题咨询
问题原因
xterm的参数规则要求,所有要在xterm终端内执行的命令,必须放在-e参数之后。你之前的写法没有加-e,xterm会把你写的二进制路径误判为自身的启动参数,所以抛出了参数不合法的错误。
修复后的代码
def name(self): os.system('xterm -bg black -fg white -geometry 150x20 -title name -fa Monospace -e /path/script-bynary /directory/ output.txt &')
补充注意点
- 原代码中
def name(self)后缺少Python语法要求的冒号:,需要补上避免语法报错 - 如果你的二进制路径、目录路径或者输出文件名包含空格,需要给对应路径加引号包裹,示例:
os.system('xterm -bg black -fg white -geometry 150x20 -title name -fa Monospace -e "/path/script binary" "/my dir/" "output name.txt" &') - 也可以替换
os.system为更安全的subprocess模块调用,避免字符串拼接的注入风险,示例:import subprocess def name(self): subprocess.Popen([ 'xterm', '-bg', 'black', '-fg', 'white', '-geometry', '150x20', '-title', 'name', '-fa', 'Monospace', '-e', '/path/script-bynary', '/directory/', 'output.txt' ])
内容的提问来源于stack exchange,提问作者esse_emme
相关产品推荐
相关产品推荐

