python exe无法生成csv文件

时间:2014-04-21 08:51:14

标签: python windows pywin32

当我从commamd提示符运行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:类全局变量包含为我的项目定义的变量,并且

没有问题

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。 错误在self .__ file_path_prefix中,它应该是os.getcwd()而不是os.path.dirname(os.path.abspath(__file__))