何を今更だけど os.system や os.popen はもう「使うな!」なんだね。
17.1. subprocess ? サブプロセス管理 ? Python 2.7ja1 documentation
それどころか Python3 には commands が無い。
subprocess の使いかたを覚えておかなければ。
# command
import os
import subprocess
#os.system("gvfs-open hoge.txt")
subprocess.call(["gvfs-open", "hoge.txt"])
# With spaces in the file names
#os.system('gvfs-open "on space.txt"')
subprocess.call(["gvfs-open", "on space.txt"])
引数の最初が args だから list に全部入れなきゃだめってことみたい。
ファイル名にスペースがある場合に楽になった、ような…
それからえっと
os.system(“gedit &”)
みたいなバックグラウンド実行はどうするのかな。
# Background #subprocess.call(["gedit", "&"]) # Error subprocess.Popen(["gedit"])
Popen を選ぶだけみたい、ふむふむ。
でコマンドのアウトプットを得るには
# Get Output
#output = os.popen("pwd").read()
#output = commands.getoutput("pwd")
output = subprocess.check_output(["pwd"])
なるほど。
ということでこんなページを作ってみた。
Python Tips – L’Isola di Niente
でも結局 GLib を使ったほうが楽だったりして。
全部 Python でやることにこだわるなんて正直アホみたい。
from gi.repository import GLib
# Execute
GLib.spawn_command_line_async("gvfs-open hoge.txt")
# Get Output
result, output, error, status = GLib.spawn_command_line_sync("ls -l")
print output