当我尝试上传位于app文件夹(app root)中的文件时,我没有任何问题,如果我想上传位于app文件夹之外的文件,即在桌面中,那么我得到了错误如下。
我发现了一个similar post帖子,但它对我没有帮助。看起来像是根本范围问题,但我对Python很新,我似乎无法找到解决这个问题的好方法。
app.root_path = os.path.dirname(os.path.abspath(__file__))
target = os.path.join(app.root_path, 'pcap/')
json_target = os.path.join(app.root_path, 'static/json/')
@app.route("/", methods=['GET','POST'])
def upload_pcap():
if request.method == 'POST':
if 'file' not in request.files :
flash('No file')
return redirect(request.url)
file = request.files['file']
if file.filename == '' :
flash('No file selected')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
cap = pyshark.FileCapture(filename)
destination = "".join([target, filename])
# create folder for pcap file if there is none
if not os.path.isdir(target):
os.mkdir(target)
# create folder for json file if there is none
if not os.path.isdir(json_target):
os.mkdir(json_target)
print(target)
for file in request.files.getlist("file"):
print(file)
print(destination)
cap.apply_on_packets(print_conversation_header,timeout=100)
print(pcap_list[8])
with open('static/json/data.json', 'w') as outfile:
json.dump(pcap_list, outfile)
file.save(destination)
#file.save(os.path.join([target, filename])
return redirect(url_for('upload_pcap', filename=filename))
return render_template('upload.html', data=pcap_list)
堆栈跟踪
Traceback (most recent call last):
File "/Users/username/PycharmProjects/untitled/venv1/lib/python3.6/site- packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File"/Users/username/PycharmProjects/untitled/venv1/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/username/PycharmProjects/untitled/venv1/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/username/PycharmProjects/untitled/venv1/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Users/username/PycharmProjects/untitled/venv1/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/username/PycharmProjects/untitled/venv1/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/username/PycharmProjects/untitled/pcap.py", line 67, in upload_pcap
cap = pyshark.FileCapture(filename)
File "/Users/username/PycharmProjects/untitled/venv1/lib/python3.6/site-packages/pyshark/capture/file_capture.py", line 48, in __init__
raise FileNotFoundError(str(self.input_filename))
FileNotFoundError: this_angler.pcap
127.0.0.1 - - [08/Feb/2018 13:28:39] "POST / HTTP/1.1" 500 -