gpt4 book ai didi

python exe无法生成csv文件

转载 作者:太空宇宙 更新时间:2023-11-03 18:26:00 25 4
gpt4 key购买 nike

当我从命令提示符运行 python 脚本时,它会生成 csv 文件,但是当我将该脚本转换为 exe 时,它​​不会生成 csv 文件。有人能告诉我问题是什么吗?

这里是生成csv文件的代码:

import os
import csv
import time
import ftplib
import threading
import database.db as db
from threading import Timer
import initial.global_variables as gv

class Csv_generator():

def __init__(self):
self.__db_conn = db.db_conn()
self.__file_name = None
self.__file_path = None
self.__file_path_prefix = os.path.dirname(os.path.abspath(__file__))
def __get_meter_data(self):
#get the kwh data from the meters from the database
localtime = time.localtime(time.time())
sys_time = "%s-%s-%s %s_00_00" %(localtime.tm_year,localtime.tm_mon,localtime.tm_mday,localtime.tm_hour)
self.__file_name = "%s_%s.csv" %(gv.org_name,sys_time)
self.__file_path = self.__file_path_prefix+ "/%s" %(self.__file_name)
self.__db_conn.cursor.execute(" SELECT m.`meter_id`FROM meter_data as m")
numrow = int(self.__db_conn.cursor.rowcount)
result=self.__db_conn.cursor.fetchall()

for x in range(0, numrow):
self.__generate_file(self.__file_path,sys_time,result[x][0])


def __generate_file(self,file_path,time,m_id,m_name,m_location,m_kwh,m_updated):

file_exist = os.path.exists(file_path)
file_ptr = open(file_path,'ab')
writer = csv.writer(file_ptr)
if file_exist == False:
writer.writerow(( "METER_TIMESTAMP",
"METER_ID"
)
writer.writerow((m_updated,m_id)


def start(self):
if gv.db_connected==1:
self.__db_conn.connect()
self.__get_meter_data()
self.__db_conn.close()

hour = int(time.strftime("%H", time.localtime()))
minute = int(time.strftime("%M", time.localtime()))
second = int(time.strftime("%S", time.localtime()))
for x in range(1,gv.upload_freq+1):
if int(hour) == 23:
hour = 0
break
if int(hour) < int(int(24/gv.upload_freq)*x):
hour = int((int(24/gv.upload_freq)*x) - int(hour) -1)
break
second = hour*3600 + (59 - minute)*60 + (60 - second)
timer_thread = Timer(second, self.start)
timer_thread.start()

ps:类全局变量包含为我的项目定义的变量,这没有问题

最佳答案

我已经解决了这个问题。错误出现在 self.__file_path_prefix 中,应该是 os.getcwd()而不是os.path.dirname(os.path.abspath(__file__))

关于python exe无法生成csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23193992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com