5

Using this general structure:

setup.py
/package
    __init__.py
    project.py
    /data
        client.log

I have a script that saves a list of names to client.log, so I don't have to reinitialize that list each time I need access to it or run the module. Before I set up this structure with pkg_resources, I used open('.../data/client.log', 'w') to update the log with explicit paths, but this doesn't work anymore.

Is there any way to edit data files within modules? Or is there a better way to save this list?

Insarov
  • 971
  • 1
  • 10
  • 15
  • 1
    It can be done but data generally shouldn't be together with your code. So consider moving it to a different directory and reading / writing from/to there. – Simeon Visser May 03 '13 at 20:07

1 Answers1

7

No, pkg_resources are for reading resources within a package. You can't use it to write log files, because it's the wrong place for log files. Your package directory should typically not be writeable by the user that loads the library. Also, your package may in fact be inside a ZIP-file.

You should instead store the logs in a log directory. Where to put that depends on a lot of things, the biggest issue is your operating system but also if it's system software or user software.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • Where are files like this usually stored in windows? And what's the best way to implement this in code? I'm guessing somewhere like `%appdata%`, or kind of arbitrarily `user\my documents`? And then I'd use an .ini or config file to map those paths upon initializing the application? Any direction/advice would help, this is all new to me. – Insarov May 03 '13 at 22:40
  • @Insarov: You would typically store them under `%APPDATA%\YourApplicationName`, yes. – Lennart Regebro May 04 '13 at 04:43
  • Where would you write these files on a mac? – Hairy Aug 07 '19 at 22:00