Ask coding questions

← Back to all posts
[discord py] How to accessing json file directory?
meziosaur (0)

Hello!
I made a discord bot that needs to access a directory.
In that directory there's a "mainbank.json" file that it reads/writes to store data.
I created the "mainbank.json" file on my replit.

This is the code in my Discord py Bot
os.chdir("D:\Mezio\Twitch\Discord\Froggo")

It's a local directory path, how can I modify this so that my bot can use that file in my replit?

Comments
hotnewtop
xfinnbar (149)

Please can I first warn you not to store data on the file system on replit, use replit's database software, repldb. I am suggesting this because data created by your program is not saved unless the editor is open. This will cause a lot of data loss.

To use replit DB, open the "packages" menu on the sidebar and enter replitapi. In your file, add from replitapi import db. Now you can use the dict-like object db as a store for all your data. It's auto saved and private.

If you don't care about that, then use a relative path. If the file is in the same folder as the bot, just use open("file name", "mode").

meziosaur (0)

@xfinnbar Thank you for the heads up, I will look into this and learn how to work with it

Coder100 (18939)

Given your file structure is like this:

folder/
  file.json

you can just do

os.chdir("folder")

you can't make it absolute like that because that's not how the replit file structure works like that.