0

I'm experimenting with Gtk+ 3 and I'm creating a program which gets the output of the dpkg --get-selections command and displays it into a Gtk+ 3 TextView.

When I run my program I get the following error:

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

This is my code:

#!/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()

Looking on StackOverflow, it appears to be a problem with subprocess , but I'm using the os module to get the dpkg command output - and the error ouput includes dpkg: error:, so maybe it's a dpkg error?

I've tried replacing the os.popen line with os.Popen(["dpkg --get-selections"], stdout=PIPE) and adding from subprocess import Popen, PIPE but I just get an error:

AttributeError: 'module' object has no attribute 'Popen'

Any ideas?

1 Answer 1

2

The dpkg --get-selections call works alright; the problem is in the following line 24 where you try to open a file with empty file name:

with open("", "w") as f:

Empty file names are not allowed. Use a "real" file name, or a temporary file of you don't want to care about names and location:

import tempfile
with tempfile.TemporaryFile() as f:

Furthermore, file has no function writeline, you're probably looking for writelines.

f.writelines(dpkg_output)
1
  • it's not the open function - I've tried that... But I'll check anyway :)
    – TellMeWhy
    Nov 29, 2015 at 11:19

Not the answer you're looking for? Browse other questions tagged .