与此问题相关
原则上这个问题是相同的,我有一个子进程系统调用
...
EDITOR = os.environ.get('EDITOR', 'vim')
subprocess.call([EDITOR, tf.name])
...
其中EDITOR
是环境变量$EDITOR
,tf.name
只是一个文件名。
然而,sublime text建议将$EDITOR
设置为export EDITOR='subl -w'
,使我的调用看起来像这样:
subprocess.call(['subl -w', "somefilename"])
它会以如下方式失败:
raceback (most recent call last):
File "/usr/bin/note", line 65, in <module>
storage["notes"][args.name] = writeNote(args.name, storage)
File "/usr/bin/note", line 54, in writeNote
subprocess.call([EDITOR, tf.name])
File "/usr/lib/python3.5/subprocess.py", line 557, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1541, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'subl -w'
当然,它应该看起来像这样:
。
subprocess.call([subl", "-w" "somefilename"])
一种解决方法可能是:
args = EDITOR.split(" ")
subprocess.call(args + ["somefilename"])
但我有点担心这样做,因为我不知道$EDITOR
设置为什么,这样安全吗?
处理这种情况的正确方式是什么?