0

当我尝试上传位于应用程序文件夹(应用程序根目录)中的文件时,我没有任何问题,但是,如果我想上传位于应用程序文件夹之外的文件,即在桌面中,那么我会收到以下错误。

我发现了一个类似的帖子,但它对我没有帮助。似乎是一个根范围问题,但是,我对 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 -
4

0 回答 0