Okay, so I’ve tried using a volume, but I don’t understand how to access it. The script uses users.txt, which is a file containing a list of names. Locally, I’ve been using root_folder\data\users.txt
.
The script adds new names to the file, and I need it to use persistent memory so that the names are always saved. I’ve created a 1 GB module and put this in my fly.toml:
[mounts]
source="bot"
destination="/data"
And here’s my code for writing to the file:
# Read users.txt
if os.path.isfile(r'/bot/data/users.txt'):
with open(r'/bot/data/users.txt', 'r') as file:
users = [line.rstrip('\n') for line in file]
# Function for writing to users.txt
def write_to_users():
try:
with open(r'/bot/data/users.txt', 'w') \
as file:
for item in users:
file.write('{}\n'.format(item))
except FileNotFoundError:
print('Current path:', os.getcwd())
print('Isdir reports:', os.path.isdir(os.getcwd()))
print('Isfile reports:', os.path.isfile(
r'/bot/data/users.txt'))
raise
My problem is that I don’t know what path to use to access users.txt. I don’t really understand how volumes work.
What I want: Store a text file called users.txt in root\data
to access in my script. It needs to be persistent, keeping the file’s contents when I re-deploy the application to update my code.
Problem: /bot/data/users.txt
doesn’t seem to work for the path in my script. I’ve also tried /data/users.txt
and data/users.txt
.
I’m getting the error FileNotFoundError: [Errno 2] No such file or directory: '/data/users.txt'
.
Umounting /dev/vdc from /data
also shows up in the console, if that means anything.
If anyone can help me out with this, that would be much, much appreciated!