0

I just deploy a python app in a free Heroku server. This python app provides a telegram bot to me. I want the bot to modify the userconfig.json which is in the same directory with main.py. When I test my bot in my local machine, it works well, the userconfig.json can be modified, but when app deploys on Heroku, it can't modify the file. After sending the modify order, there is no change on my userconfig.json. How can I solve this problem?

Below is the member method which modifies the json file

    def  modify_token(self, new_token):
        self.tokenArray[0] = new_token
        token_update = [{'token': self.tokenArray[0]}]
        try:
            with open("./userconfig.json", 'w') as config:
                json.dump(token_update, config)
        except Exception as e:
            return False

        return True

And I use these codes to ask telegram bot to modify the file


def modify(update: Update, context: CallbackContext):
    # print(context)
    print(context.args)
    try:
        user_cake.modify_token(context.args[0])
    except Exception as e:
        err_res = str(e)
        context.bot.send_message(chat_id=update.effective_chat.id, text="An error occurred:" + err_res)
    else:
        context.bot.send_message(chat_id=update.effective_chat.id, text="Your new token is: " + user_cake.tokenArray[0])
Mike
  • 3
  • 2
  • Hi. Do you get any error messages? If yes, please share the tracebacks. Can it be a file permisison problem? Can you show the relevant part of your code? Please have a look at [this guide](https://stackoverflow.com/help/how-to-ask) on how to formulate well-posed questions :) – CallMeStag Apr 23 '21 at 14:30
  • This isn't the immediate problem, but you shouldn't be modifying data on the local filesystem. See [Can I use a file based database on Heroku?](https://stackoverflow.com/q/50421061/354577), for example. – ChrisGPT was on strike Apr 23 '21 at 14:32
  • @CallMeStag there are no error messages output and `userconfig.json`have `wirte` permission. – Mike Apr 24 '21 at 07:03
  • @Chris You are right, but I'm just curious about why I can't modify my file on heroku but can modify my file on local when the app runs – Mike Apr 24 '21 at 07:10
  • @Mike Maybe you just don't see the errors because you `try-except` them away in your `modify_token` function? – CallMeStag Apr 24 '21 at 07:33

0 Answers0