私はGtk + 3を試していて、dpkg --get-selections
コマンドの出力を取得してGtk +3に表示するプログラムを作成していますTextView
。
プログラムを実行すると、次のエラーが発生します。
Traceback (most recent call last):
File "file1.py", line 36, in <module>
window = dpkgApp()
File "file1.py", line 24, in __init__
with open("", "w") as f:
IOError: [Errno 2] No such file or directory: ''
dpkg: error: error writing to '<standard output>': Broken pipe
これは私のコードです:
#!/usr/bin/python
import io, subprocess, os
from gi.repository import Gtk
class dpkgApp(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Software/dependencies")
self.table = Gtk.Table(3, 3, True)
self.add(self.table)
self.scrollWindow = Gtk.ScrolledWindow()
self.table.attach(self.scrollWindow, 0, 1, 0, 1)
self.textView = Gtk.TextView()
self.scrollWindow.add(self.textView)
#######################################################################
subprocess.call("dpkg --get-selections", shell=True)
dpkg_output = os.popen("dpkg --get-selections")
with open("", "w") as f:
f.writeline(dpkg_output)
f.close()
buffer = Gtk.TextBuffer()
self.textView.get_buffer(buffer)
self.textView.set_editable(False)
self.textView.set_wrap_mode(True)
self.textView.set_cursor_visible(False)
buffer.set_text(dpkg_output)
window = dpkgApp()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
StackOverflowを見ると、に問題subprocess
があるようですが、os
モジュールを使用してdpkg
コマンド出力を取得しています-エラー出力にはが含まれているdpkg: error:
ので、エラーである可能性がありdpkg
ますか?
os.popen
行をに置き換えてos.Popen(["dpkg --get-selections"], stdout=PIPE)
追加しようとしましたfrom subprocess import Popen, PIPE
が、エラーが発生します:
AttributeError: 'module' object has no attribute 'Popen'
何か案は?
dpkg --get-selections
コールは大丈夫動作します。問題は、空のファイル名でファイルを開こうとする次の24行目にあります。
with open("", "w") as f:
空のファイル名は許可されていません。「実際の」ファイル名、または名前と場所を気にしたくない一時ファイルを使用します。
import tempfile
with tempfile.TemporaryFile() as f:
さらに、file
機能がありませんwriteline
、あなたはおそらく探していwritelines
ます。
f.writelines(dpkg_output)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加