Question

Importing .json/.txt files in my Python application (built with GitHub)

Hello, I am looking for a method on how to properly load-in and edit small data storage files like .json or a .txt (I know data storage with json files is bad practice, I am working on MySQL implementation). Right now my application gets the main.py and .txt files from my GitHub repo’s. However, it is not able to edit any of these .txt files. How would I go about achieving what I want?

Thank you in advance!


Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
March 2, 2023

Hi there,

You should be able to do that with the standard Python modules like json and os to read and write to the files.

You can read the file with:

with open('data.json', 'r') as f:
     data = json.load(f)

To write to a file, you can use the w flag:

with open("data_file.json", "w") as write_file:
    json.dump(data, write_file)

Note that if you are using the DigitalOcean App Platform, this will not work, as the storage there is ephemeral, meaning that you will loose all of your changes after each new build.

Feel free to share more details on your setup and I will be happy to advise you further!

Best,

Bobby