首页 > 解决方案 > Python tempfile NamedTemporaryFile 文件在尝试访问它以进行读取时未找到

问题描述

无法访问临时 csv 文件并且它给出的错误是 MySQLdb._exceptions.InternalError: (2, "File '/tmp/tmpc1hsxls9' not found (Errcode: 2 - No such file or directory)")。任何有关它的建议都将是非常可观的。

 tmpfile = None
 with NamedTemporaryFile("wb") as tmpfile:
     self.log.info("Selecting rows from MySql to local file %s...", tmpfile.name)
     self.log.info("select mtime, dpid, route_id, skid, `group`, family, date, sale, dprice, rprice, dcc_price, issue, `return`, memos, vmemos, tlp, cnc, vp, mvp, p, tcc, dcc, ecnc, gt, struc, semi_struc, streetk, mass_hrc, pop_hrc, prem_hrc, kaccounts, ogrocery, snb_cnc, pay_n_go, shop_n_browse, entertainment, outlets, apps_version, updated, visited from daily_sales_msr where date between '"+day+"' and '"+day+"'")

     # self.log.info("select mtime, dpid, route_id, skid, group, family, date, datetime, sale, dprice, rprice, dcc_price, issue, return, memoss, vmemos, tlp, cnc, vp, mvp, p, tcc, dcc, ecnc, gt, struc, semi_struc, streetk, mass_hrc, pop_hrc, prem_hrc, kaccounts, ogrocery, snb_cnc, pay_n_go, shop_n_browse, entertainment, outlets, apps_version, updated, visited from daily_sales_msr where month(date) = month("+month+") and year(date) = year("+month+")")
     # self.log.info(self.sql)

     csv_writer = csv.writer(tmpfile, delimiter='\t', encoding='utf-8')
     result = cursor.fetchall()
     self.log.info(result)
     for row in result:
       self.log.info(row)
       csv_writer.writerow(row)
       count += 1

     tmpfile.flush()

 with closing(mysql_destination.get_conn()) as conn:
    with closing(conn.cursor()) as cursor:
      cursor.execute(
       "LOAD DATA LOCAL INFILE '%s' INTO "
       "TABLE %s LINES TERMINATED BY '\r\n' (%s)" %
       (tmpfile.name,
        self.mysql_destination_table,
        ", ".join(selected_columns))
       )
       conn.commit()
  tmpfile.close()

标签: mysqlpython-3.xmariadb

解决方案


推荐阅读