首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用Python脚本,但不从批处理文件执行。

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

Stack Overflow用户
提问于 2016-08-11 09:06:08
回答 1查看 942关注 0票数 0

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

代码语言:javascript
复制
@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文件,但没有成功。最理想的是,我希望它能保持窗口打开,并使用脚本中的所有日志记录,但这是另一个可以稍后解决的问题。

代码语言:javascript
复制
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

函数按以下顺序调用

代码语言:javascript
复制
    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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-11 12:48:54

好的,似乎脚本中的.exe返回了一个错误,这就是为什么脚本完成得这么快的原因。我以为蝙蝠的档案没有等待。我应该将.bat文件放在.exe文件夹中,现在整个过程运行得非常完美。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38891928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档