我如何使用来自pythonanywhere上托管的flask应用程序的输入将信息写入txt文件或csv文件中

问题描述 投票:0回答:1

我不明白问题所在。在我在终端中运行此代码之前,它工作正常,但是当我将其托管在pythonanywhere上时,此代码不会将html表单的输入写入txt文件。我对发生的事情感到非常困惑。我的python代码是这个

def write_file(data):
    with open('new.txt', 'a') as f:
        f.write(data + '\n')


@app.route('/in/', methods=['GET', 'POST'])
def notin():
    if request.method == 'POST':
        notin = str(request.form['in'])
        write_file(notin)
        return render_template('in.html')
    return render_template('form.html')

我的html代码是(in.html):

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
 <form method="POST" action='/in/'>
      <div class="form-group">
        <input type="text" name="in">
      </div>
    </form>

  </body>
</html>
python flask pythonanywhere
1个回答
0
投票

您正在使用文件的相对路径,而不关注代码运行所在的工作目录。使用new.txt的完整路径,以使工作目录不影响文件的写入位置。

© www.soinside.com 2019 - 2024. All rights reserved.