ARS RMS
ARS RMS

Reputation: 1

Python: FileNotFoundError: [WinError 3] The system cannot find the path specified:

I am trying to figure out why my output was ending early when listing directories on from my hard drive to a website. I removed the error message for debugging mode and got this error:

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\Huang.LabTech2\\Desktop\\Images\\2017 Data\\Air Tractor 402/Air Tractor 402/5-10-2017/IR Filter'

I'm not sure why it is looking for the second Air Tractor 402 folder when running, but this happens with other folders as well:

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\Huang.LabTech2\\Desktop\\Images\\2017 Data\\UAV/UAV/Bobby_Hardin/8-16'

My code:

from flask import Flask, abort, send_file, render_template, render_template_string
import os
import remoteSensingData

app = Flask(__name__)

@app.route('/', defaults={'req_path': ''})
@app.route('/<path:req_path>')
def dir_listing(req_path):
    BASE_DIR = os.path.abspath(r'C:/Users/Huang.LabTech2/Desktop/Images/2017 Data/')

    # Joining the base and the requested path
    abs_path = os.path.join("c:", BASE_DIR, req_path)

    # Return 404 if path doesn't exist
    # if not os.path.exists(abs_path):
    #     return render_template('404.html'), 404

    # Check if path is a file and serve
    # if os.path.isfile(abs_path):
    #     return send_file(abs_path)

    # Show directory contents
    files = os.listdir(abs_path)
    files = [os.path.join(req_path, file) for file in files]
    # return render_template('files.html', files=files)
    return render_template_string("""
    <ul>
        {% for file in files %}
        <li><a href="{{ file }}">{{ file }}</a></li>
        {% endfor %}
    </ul>
    """, files=files)


if __name__ == '__main__':
  app.run(debug=True)

Upvotes: 0

Views: 4728

Answers (1)

DDS
DDS

Reputation: 2478

You got the "repetition" because you add the req_path two times: one here:

# Joining the base and the requested path
    abs_path = os.path.join("c:", BASE_DIR, req_path)

and one here:

files = [os.path.join(req_path, file) for file in files]

the second should look like this:

files = [f for f in files] # (or just be deleted)

Upvotes: 2

Related Questions