简体   繁体   English

来自 python 目录的 Pandas.read_csv 文件

[英]Pandas.read_csv file from directory python

i need to read_csv file in python which file is in the UPLOAD_FOLDER我需要在python中读取_csv文件哪个文件在UPLOAD_FOLDER

FileNotFoundError: [Errno 2] File b'../bpe.csv' does not exist:

this the code:这是代码:

def get_uploads():
    """gets the images and other files from the uploads directory
    and returns two lists of tuples in the form (name, path)
    """

    others = []

    uploads = os.listdir(os.path.join(app.root_path, app.config['UPLOAD_FOLDER']))

    # sort by time last modified
    uploads.sort(key=lambda filename: os.stat(os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], filename)).st_mtime)

    for upload in uploads[::-1]:
        others.append(( upload))
    print(others)
    for i in others:
        if i=='bpe.csv':
            databpe=i
        if  i=='ch.csv':
            datach=i

    if databpe and  datach:

        df=pandas.read_csv(databpe)
        ch=pandas.read_csv(datach)
        flash("check desktop","success")
    return render_template("page3.html")

os.listdir returns relative paths. os.listdir返回相对路径。 So a minimal fix would be:所以一个最小的修复是:

    df=pandas.read_csv(os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], databpe))
    ch=pandas.read_csv(os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], datach))

A better refactoring would be:更好的重构是:

def get_uploads():
    databpe_path = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], 'bpe.csv')
    datach_path = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'], 'ch.csv')

    if os.path.isfile(databpe_path) and os.path.isfile(datach_path):
        df=pandas.read_csv(databpe_path)
        ch=pandas.read_csv(datach_path)
        flash("check desktop","success")

    return render_template("page3.html")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM