为了账号安全,请及时绑定邮箱和手机立即绑定

来自 python 目录的 Pandas.read_csv 文件

来自 python 目录的 Pandas.read_csv 文件

德玛西亚99 2022-01-18 17:07:30
我需要在python中读取_csv文件,哪个文件在 UPLOAD_FOLDERFileNotFoundError: [Errno 2] File b'../bpe.csv' does not exist:这是代码: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")
查看完整描述

1 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

os.listdir返回相对路径。所以一个最小的修复是:


    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))

更好的重构是:


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")


查看完整回答
反对 回复 2022-01-18
  • 1 回答
  • 0 关注
  • 203 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号