簡體   English   中英

如何使用來自 pythonanywhere 上托管的 flask 應用程序的輸入將信息寫入 txt 文件或 csv 文件

[英]How do I write info into a txt file or csv file using input from a flask app that is hosted on pythonanywhere

我不明白這個問題。 之前,當我在終端中運行這段代碼時,它工作正常,但是當我將它托管在 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>

您正在使用文件的相對路徑,而沒有注意運行代碼的工作目錄。 使用 new.txt 的完整路徑,這樣工作目錄就不會影響文件的寫入位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM