简体   繁体   中英

Python script is called but not executed from batch file

i have a python script that when is run from eclipse it does what i want without any errors or anything. I want now to create a batch file, that will run my script in a loop (infinitely). The first problem is that i when i run the bat file, i get a second cmd window that shows the logging from my python script (which shows me that it is running) but when the main process of the script starts(which can take from 1 minute to some hours) it exits within a few second without actually running all the script. I have used start wait/ but it doesn't seem to work. Here is the simple batch file i have created:

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

So i want the bat file to run my script, wait for it to finish(even if it takes some hours) and then run it again. I have also tried creating a bat file that calls with start wait/ the bat file shown above with no success. Optimally i would like it to keep the window open with all the logging that i have in my script, but that is another issue that can be solved later.

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

The functions are called in this order

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

Ok it seems that the .exe in the script was returning an error and that is why it the script was finishing so fast. I thought that the bat file did not wait. I should have placed the .bat file in the .exe folder and now the whole thing runs perfect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM