Aze*_*rah 4 python command-line environment-variables sublimetext3
与这个问题相关
原则上的问题是相同的,我有一个 subprocess.system 调用
...
EDITOR = os.environ.get('EDITOR', 'vim')
subprocess.call([EDITOR, tf.name])
...
Run Code Online (Sandbox Code Playgroud)
EDITOR环境变量在哪里$EDITOR,tf.name只是一个文件名。
但是,sublime text建议将设置为“$EDITOR使export EDITOR='subl -w'我的通话看起来像这样”:
subprocess.call(['subl -w', "somefilename"])
Run Code Online (Sandbox Code Playgroud)
它失败了,如下所示:
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'
Run Code Online (Sandbox Code Playgroud)
当然,它应该看起来像这样
subprocess.call([subl", "-w" "somefilename"])
Run Code Online (Sandbox Code Playgroud)
解决方法也许是
args = EDITOR.split(" ")
subprocess.call(args + ["somefilename"])
Run Code Online (Sandbox Code Playgroud)
但我对此有点谨慎,因为我不知道$EDITOR设置的是什么,这样做安全吗?
处理此案的正确方法是什么?
小智 5
你可以使用 shlex。它负责类似 UNIX shell 的命令。例如:
>>> shlex.split( "folder\ editor" ) + ["somefilename"]
['folder editor', 'somefilename']
>>> shlex.split( "editor -arg" ) + ["somefilename"]
['editor', '-arg', 'somefilename']
所以你应该能够直接执行以下操作:
subprocess.call( shlex.split(EDITOR) + ["somefilename"] )
| 归档时间: |
|
| 查看次数: |
1335 次 |
| 最近记录: |