python-3.x - 为什么 Heroku 不保存我的代码写入文件的更改?
问题描述
如果我在我的机器上运行我的代码,当我使用我的代码将内部的数字增加一时,我使用我的代码写入文件的更改count.txt
将成功写入count.txt
. 但是,当我的应用程序在 heroku 上运行时,对文件的更改既不会进行也不会保存。我打开终端,执行 bash,cat count.txt
并在我的服务器中的用户使用机器人时读取内容count.txt
:文件没有更改。我手动输入文件的数字仍然存在,没有改变。发生这种情况是否有原因,是否有解决方案?
import discord
from discord.ext import commands
import re
class Counting(commands.Cog):
""" Users take turns incrementing a number. """
def __init__(self, client):
self.client = client
self.count_channel = 761981805862846525
self.num_regex = re.compile(r'(^\d+)')
self.cum_num = None
self.filename = "count.txt"
@commands.Cog.listener()
async def on_message(self, message):
with open(self.filename) as f:
read_data = f.read()
self.cum_num = int(read_data)
if message.channel.id == self.count_channel:
try:
match_obj = self.num_regex.search(message.content)
if int(match_obj.group(1)) != self.cum_num + 1:
await message.author.send(f'Your message must start with **{self.cum_num + 1}**!')
await message.delete()
else:
self.cum_num += 1
with open(self.filename, 'w') as f:
f.write(str(self.cum_num))
except AttributeError:
if message.author.id != self.client.user.id:
await message.delete()
await message.author.send(f"Your message must be a number!")```
解决方案
Heroku 不支持这样的事情。请改用在线数据库。(例如,我使用的是MongoDB)。
祝你好运!
推荐阅读
- machine-learning - python中的字符分割
- python - 为 lxml 的构建依赖项添加到源列表中的内容
- wordpress - Timber - 用于渲染插件模板
- google-bigquery - BigQuery 中的 OTRANSLATE 等价物是什么?
- javascript - Understanding how javascript evaluates the OR operator
- android - How to add text to another layout without starting a new layout?
- ruby-on-rails - 缺少主机以链接到 Rails 5.2.1 中的错误
- angular - Angular 7 上传到带有进度指示器的 blob 存储
- multithreading - Atomic Operations for Multithreading in SBCL
- python - Difference between a numpy.array and numpy.array[:]