捕获 STDOUT 以协调文件名

Capturing STDOUT to then reconcile on the file names

我一直在努力解决这个问题,我正在尝试创建一个程序,它将根据当前日期和时间创建一个 datetime 对象,从我们的文件中创建第二个这样的对象数据,找出两者之间的差异,如果大于 10 分钟,则搜索 "handshake file",这是我们成功加载文件后收到的文件。如果我们找不到该文件,我想发出一封错误电子邮件。

我的问题在于能够以一种有意义的方式捕获我的 ls 命令的结果,以便我能够解析它并查看是否存在正确的文件。这是我的代码:

"""
This module will check the handshake files sent by Pivot based on the following conventions:
- First handshake file (loaded to the CFL, *auditv2*): Check every half-hour
- Second handshake file (proofs are loaded and available, *handshake*): Check every 2 hours
"""
import smtplib
from email.mime.text import MIMEText
from datetime import datetime, timedelta
from csv import DictReader
from subprocess import *
from os import chdir
from glob import glob


def main():
    audit_in = '/prod/bcs/lgnp/clientapp/csvbill/audit_process/lgnp.smr.csv0000.audit.qty'
    with open(audit_in, 'rbU') as audit_qty:    
        my_audit_reader = DictReader(audit_qty, delimiter=';', restkey='ignored')
        my_audit_reader.fieldnames = ("Property Code",
                                      "Pivot ID", 
                                      "Inwork File", 
                                      "Billing Manager E-mail", 
                                      "Total Records", 
                                      "Number of E-Bills", 
                                      "Printed Records", 
                                      "File Date", 
                                      "Hour", 
                                      "Minute", 
                                      "Status")

        # Get current time to reconcile against
        now = datetime.now()

        # Change internal directory to location of handshakes
        chdir('/prod/bcs/lgnp/input')   

        for line in my_audit_reader:
            piv_id = line['Pivot ID']
            status = line['Status']
            file_date = datetime(int(line['File Date'][:4]),
                                 int(line['File Date'][4:6]),
                                 int(line['File Date'][6:8]),
                                 int(line['Hour']),
                                 int(line['Minute']))
            # print(file_date)
            if status == 's':
                diff = now - file_date
                print diff
                print piv_id
                if 10 < (diff.seconds / 60) < 30:
                    proc = Popen('ls -lh *{0}*'.format(status),
                                 shell=True) # figure out how to get output

                    print proc




def send_email(recipient_list):
    msg = MIMEText('Insert message here')
    msg['Subject'] = 'Alert!! Handshake files missing!'
    msg['From'] = r'xxx@xxx.com'
    msg['To'] = recipient_list

    s = smtplib.SMTP(r'xxx.xxx.xxx')
    s.sendmail(msg['From'], msg['To'], msg.as_string())
    s.quit()


if __name__ == '__main__':
    main()

解析 ls 输出并不是最好的解决方案。你当然可以解析 subprocess.check_output 结果或以任何其他方式,但让我给你一个建议。

如果您发现自己通过解析某人的输出或日​​志来解决标准问题,这是一个很好的出错标准,请考虑其他解决方案,如下所示:

如果您只想查看目录的内容,请使用 os.listdir,例如:

my_home_files = os.listdir(os.path.expanduser('~/my_dir')) # surely it's cross-platform

现在您的 my_home_files 变量中有一个文件列表。 您可以按照您想要的方式过滤它们,或者使用 glob.glob 来使用这样的元字符:

glob.glob("/home/me/handshake-*.txt") # will output everything matching the expression 
# (say you have ids in your filenames).

之后您可能想检查文件的一些统计信息(如上次访问日期等) 考虑使用 os.stat:

os.stat(my_home_files[0]) # outputs stats of the first
# posix.stat_result(st_mode=33104, st_ino=140378115, st_dev=3306L, st_nlink=1, st_uid=23449, st_gid=59216, st_size=1442, st_atime=1421834474, st_mtime=1441831745, st_ctime=1441234474)
# see os.stat linked above to understand how to parse it