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


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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel