我正嘗試將上傳的文件保存在我的系統上。在特定的路徑,但我得到這個錯誤在Windows中。有人能告訴我我在哪裏做錯了嗎?使用werkzeug在文件系統上保存文件
操作系統:Windows 8.1 Python版本:2.7
這裏是我的代碼:
# -*- coding: utf-8 -*-
from werkzeug.serving import run_simple
from werkzeug.wrappers import BaseRequest, BaseResponse
import os
def view_file(req):
if not 'file' in req.files:
return BaseResponse('no file uploaded')
f = req.files['file']
s = "C:\Users\admin\Desktop\test"
f.save(s, f.filename)
return BaseResponse('File Saved!')
def upload_file(req):
return BaseResponse('''
<h1>Upload File</h1>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
''', mimetype='text/html')
def application(environ, start_response):
req = BaseRequest(environ)
if req.method == 'POST':
resp = view_file(req)
else:
resp = upload_file(req)
return resp(environ, start_response)
if __name__ == '__main__':
run_simple('localhost', 5000, application, use_debugger=True)
這裏是回溯:
Traceback (most recent call last):
File "C:\Users\admin\Desktop\test.py", line 30, in application
resp = view_file(req)
File "C:\Users\admin\Desktop\test.py", line 13, in view_file
f.save(s, f.filename)
File "C:\Python27\lib\site-packages\werkzeug\datastructures.py", line 2703, in
save
dst = open(dst, 'wb')
IOError: [Errno 22] invalid mode ('wb') or filename: 'C:\\Users\x07dmin\\Desktop
\test'
你需要逃避'''\'''在你的文件的路徑。 –
更改s =「C:\\ Users \\ admin \\ Desktop \\ test」後,現在變爲IOError:[Errno 13]權限被拒絕:'C:\\ Users \\ admin \\ Desktop \\ test' –