Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
def loadSubredditConfig (self):
    self.builder.get_object ('cbxR').remove_all ()
    path = os.path.join (xdg.BaseDirectory.xdg_config_home, 'RedThat', 'subreddits')

    if (os.path.exists (path)):
        file = open (path, 'r+')
        for line in file.readLines ():
            self.builder.get_object ('cbxR').append_text (line.strip ().replace (' ', ''))
        file = open (path, 'w') # Problem here
        file.write ('')
        self.builder.get_object ('cbxR').append_text ('')

Traceback (most recent call last):
  File "bin/gtkreddit", line 33, in <module>
  File "/home/robin/Ubuntu One/Python/gtkreddit/gtkreddit/", line 33, in main
    window = GtkredditWindow.GtkredditWindow()
  File "/home/robin/Ubuntu One/Python/gtkreddit/gtkreddit_lib/", line 35, in __new__
  File "/home/robin/Ubuntu One/Python/gtkreddit/gtkreddit/", line 63, in finish_initializing
    self.loadSubredditConfig ()
  File "/home/robin/Ubuntu One/Python/gtkreddit/gtkreddit/", line 236, in loadSubredditConfig
    file = open (path, 'w')
IOError: [Errno 2] No such file or directory: '/home/robin/.config/RedThat/subreddits'

I want the file to be created.
Tried w, a, rw, w+, a+. All throw the same error.

share|improve this question
up vote 2 down vote accepted

Make sure the directory /home/robin/.config/RedThat/ exists before trying to create a file in it.

share|improve this answer
if (not os.path.exists (folder)): os.makedirs (folder) – RobinJ Jul 1 '12 at 18:06

You need to ensure all the parent directories exist, to the path of where you want to create a file. You can use os.makedirs to accomplish this. You also should probably avoid using xdg.BaseDirectory as python-xdg isn't really maintained any more upstream. You can use dirspec.basedir instead, which is the python-dirspec package in Ubuntu.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.