【问题标题】:Capturing STDOUT to then reconcile on the file names捕获 STDOUT 以协调文件名
【发布时间】:2015-09-09 21:52:42
【问题描述】:

我一直在努力解决这个问题,我正在尝试创建一个程序,该程序将根据当前日期和时间创建一个 datetime 对象,从我们的文件数据中创建第二个这样的对象,找到两者之间的区别,如果大于 10 分钟,则搜索“握手文件”,这是我们在文件成功加载后收到的文件。如果我们没有找到那个文件,我想踢出一封错误邮件。

我的问题在于能够以一种有意义的方式捕获我的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。曾经。只需使用os.path 和朋友。
  • 赞同凯文所说的话。正确的做法是使用os.listdir 和(可能)os.isfile
  • 谢谢先生们,所以 subprocess 似乎无关紧要,除非您需要使用 Popen 功能...

标签: python subprocess python-2.6


【解决方案1】:

在这里解析 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 2019-11-14
    • 2013-01-12
    • 2014-08-05
    相关资源
    最近更新 更多