Python: writing to files inside packages?

Using this general structure:

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

I have a script that saves a list of names in client.log , so I donโ€™t need to reinitialize this list every time I need to access it or launch a module. Before setting up this structure with pkg_resources , I used open('.../data/client.log', 'w') to update the log with explicit paths, but this no longer works.

Is there a way to edit data files inside modules? Or is there a better way to keep this list?

+4
source share
1 answer

No, pkg_resources are for reading resources inside a package. You cannot use it to write log files because this is the wrong place for log files. Usually your directory directory cannot be written by the user who is loading the library. In addition, your package may be inside a ZIP file.

Instead, you should store the logs in the log directory. When it depends on many factors, the biggest problem is your operating system, but also the system software or user software.

+4
source

Source: https://habr.com/ru/post/1479057/


All Articles