1
我有一個基於瓶的Web服務,我試圖將結果下載到用戶桌面的文件(通過https)。Python:如何寫入文件並下載它?
我想:當我點擊用戶界面導出按鈕
def write_results_to_file(results):
with open('output', 'w') as f:
f.write('\t'.join(results[1:]) + '\n')
這種方法被激活。
但我得到:
<type 'exceptions.IOError'>: [Errno 13] Permission denied: 'output'
args = (13, 'Permission denied')
errno = 13
filename = 'output'
message = ''
strerror = 'Permission denied'
有人能告訴我什麼,我做錯了什麼?
聽起來像一個文件系統權限問題。您使用什麼操作系統?另外,您是在本地寫入文件還是將它發送迴響應中? – sakurashinken
我正在嘗試在本地寫入,然後將其發送迴響應。不知道這是否正確,但 – user3407267
您要在標題中指定內容類型,然後將文件發送回主體。 flask提供了send_file方法。無需寫入本地FS。 https://stackoverflow.com/questions/27337013/how-to-send-zip-files-in-the-python-flask-framework – sakurashinken