Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.
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 (' ', ''))
    else:
        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>
    gtkreddit.main()
  File "/home/robin/Ubuntu One/Python/gtkreddit/gtkreddit/__init__.py", line 33, in main
    window = GtkredditWindow.GtkredditWindow()
  File "/home/robin/Ubuntu One/Python/gtkreddit/gtkreddit_lib/Window.py", line 35, in __new__
    new_object.finish_initializing(builder)
  File "/home/robin/Ubuntu One/Python/gtkreddit/gtkreddit/GtkredditWindow.py", line 63, in finish_initializing
    self.loadSubredditConfig ()
  File "/home/robin/Ubuntu One/Python/gtkreddit/gtkreddit/GtkredditWindow.py", 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
add comment

2 Answers 2

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
2  
if (not os.path.exists (folder)): os.makedirs (folder) –  RobinJ Jul 1 '12 at 18:06
add comment

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
add comment

Your Answer

 
discard

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.