0

When I try to upload a file that is located in the app folder (app root) I have no issues however, if I want to upload a file that sits outside the app folder, i.e. in the Desktop, then I get the error below.

I found a similar post post but it did not help me. Seems like a root scoping issue however, I'm fairly new to Python and I can't seem to find a good solution to fix this.

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)

The stack trace

    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 -
  • `FileNotFoundError: this_angler.pcap` seems that you have a file in the current dir, but the current dir isn't the one you think it is. – Jean-François Fabre Feb 08 '18 at 14:48
  • I think the path that is given to the file upon upload is posting to the app root and not to the directory root for that file. I tried doing something like `cap = pyshark.FileCapture(os.path.dirname(os.path.abspath(filename)))` to pass the absolute path to pyshark however it won't work. Pyshark needs an absolute path, in this case the path has to be the one for that file I'm uploading. – wolfishCoder Feb 08 '18 at 14:59
  • using the variable `filename` or using `file` return the same error. – wolfishCoder Feb 08 '18 at 15:27

0 Answers0