调用Python脚本但不从批处理文件执行

时间:2016-08-11 09:06:09

标签: python batch-file

我有一个python脚本,当从eclipse运行时,它做我想要的没有任何错误或任何东西。 我现在想创建一个批处理文件,它将循环运行我的脚本(无限)。 第一个问题是,当我运行bat文件时,我得到第二个cmd窗口,显示来自我的python脚本的日志记录(显示它正在运行)但是当脚本的主进程启动时(可以从1分钟到几个小时)它在几秒钟内退出而没有实际运行所有脚本。我用过开始等待/但它似乎没有用。这是我创建的简单批处理文件:

@echo off
:start
start /wait C:\Python32\python.exe  C:\Users\some_user\workspace\DMS_GUI\CheckCreateAdtf\NewTest.py
goto start

所以我希望bat文件运行我的脚本,等待它完成(即使需要几个小时),然后再次运行它。 我还尝试创建一个bat文件,使用上面显示的start wait / bat文件调用但没有成功。 最好我希望它保持窗口打开我的脚本中的所有日志记录,但这是另一个可以在以后解决的问题。

def _open_read_file(self):
    logging.debug("Checking txt file with OLD DB-folder sizes")
    content = []

    with open(self._pathToFileWithDBsize) as f:
        content = f.read().splitlines()

    for p in content:
        name,size = (p.split(","))
        self._folder_sizes_dic[name] = size

def _check_DB(self):
    logging.debug("Checking current DB size")
    skippaths =  ['OtherData','Aa','Sss','asss','dss','dddd']

    dirlist = [ item for item in os.listdir(self._pathToDBparentFolder) if os.path.isdir(os.path.join(self._pathToDBparentFolder, item)) ]

    for skip in skippaths:
        if skip in dirlist:
            dirlist.remove(skip)

    MB=1024*1024.0
    for dir in dirlist:
        folderPath = self._pathToDBparentFolder +"\\"+str(dir)
        fso = com.Dispatch("Scripting.FileSystemObject")
        folder = fso.GetFolder(folderPath)
        size = str("%.5f"%(folder.Size/MB))
        self._DB_folder_sizes_dic[dir] = size


def _compare_fsizes(self):

    logging.debug("Comparing sizes between DB and txt file")
    for (key, value) in self._DB_folder_sizes_dic.items():
        if key in self._folder_sizes_dic:
            if (float(self._DB_folder_sizes_dic.get(key)) - float(self._folder_sizes_dic.get(key)) < 100.0 and float(self._DB_folder_sizes_dic.get(key)) - float(self._folder_sizes_dic.get(key)) > -30.0):
                pass
            else:
                self._changed_folders.append(key)
        else:
            self._changed_folders.append(key)


def _update_file_with_new_folder_sizes(self):

    logging.debug("Updating txt file with new DB sizes")
    file = open(self._pathToFileWithDBsize,'w')
    for key,value in self._DB_folder_sizes_dic.items():
        file.write(str(key)+","+str(value)+"\n")


def _create_paths_for_changed_folders(self):

    logging.debug("Creating paths to parse for the changed folders")
    full_changed_folder_parent_paths = []

    for folder in self._changed_folders:
        full_changed_folder_parent_paths.append(self._pathToDBparentFolder +"\\"+str(folder))

    for p in full_changed_folder_parent_paths:
        for path, dirs, files in os.walk(p):
            if not dirs:
                self._full_paths_to_check_for_adtfs.append(path)


def _find_dat_files_with_no_adtf(self):

        logging.debug("Finding files with no adtf txt")

        for path in self._full_paths_to_check_for_adtfs:
            for path, dirs, files in os.walk(path):
                for f in files:
                    if f.endswith('_AdtfInfo.txt'):
                        hasAdtfFilename = f.replace('_AdtfInfo.txt', '.dat')
                        self.hasADTFinfos.add(path + "\\" + hasAdtfFilename)
                        self.adtf_files = self.adtf_files + 1
                    elif f.endswith('.dat'):
                        self.dat_files = self.dat_files + 1
                        self._dat_file_paths.append(path + "\\" +  f)


        logging.debug("Checking which files have AdtfInfo.txt, This will take some time depending on the number of .dat files ")


        for file in self._dat_file_paths:
            if file not in self.hasADTFinfos:
                self._dat_with_no_adtf.append(file)

        self.files_with_no_adtf = len(self._dat_with_no_adtf)
        #self.unique_paths_from_log = set(full_paths_to_check_for_adtfs)

        logging.debug("Files found with no adtf " + str(self.files_with_no_adtf))



def _create_adtf_info(self):
        logging.debug("Creating Adtf txt for dat files")
        files_numbering = 0
        for file in self._dat_with_no_adtf:
            file_name = str(file)

            adtf_file_name_path = file.replace('.dat','_AdtfInfo.txt')
            exe_path = r"C:\Users\some_user\Desktop\some.exe "
            path_to_dat_file = file_name
            path_to_adtf_file = adtf_file_name_path
            command_to_subprocess = exe_path + path_to_dat_file + " -d "+ path_to_adtf_file 

            #Call VisionAdtfInfoToCsv
            subprocess.Popen(command_to_subprocess,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
            process_response = subprocess.check_output(command_to_subprocess)
            #if index0 in response, adtf could not be created because .dat file is probably corrupted
            if "index0" in str(process_response):
                self._corrupted_files_paths.append(path_to_dat_file)
                self._files_corrupted = self._files_corrupted + 1
                self._corrupted_file_exist_flag = True
            else:
                self._files_processed_successfully = self._files_processed_successfully + 1

            files_numbering = files_numbering + 1

按此顺序调用函数

    self._open_read_file()
    self._check_DB()
    self._compare_fsizes()
    self._create_paths_for_changed_folders()
    self._find_dat_files_with_no_adtf()
    self._create_adtf_info()
    self._check_DB()
    self._update_file_with_new_folder_sizes()

1 个答案:

答案 0 :(得分:0)

好吧,似乎脚本中的.exe返回错误,这就是为什么脚本完成得如此之快。我以为bat文件没等。我应该将.bat文件放在.exe文件夹中,现在整个过程都很完美。