[Tutor] pass arg to list
Hi, I'm trying to pass arguments to list. This script needs to generate csv files. I want to pass the version and build number to the script and it should be passed to the list. My script below is blowing index out of range after adding the args. However, if I hardcode the version, it works fine. Kindly advise. # User Args version = sys.argv[1] build = sys.argv[2] add_file = ( ['AddFile', 109102010, 'Product-'+version + 'Package (Build' + build + ')', 'Product-'+version+'-b'+build+'-package.pkg', 'Product-'+version + 'Service Package (Build' + build + ')', version, ''], ) Traceback (most recent call last): File "/Users/atamsekar/Projects/PulseSO/trunk/swrelease/samplexls.py", line 5, in version = sys.argv[1] IndexError: list index out of range Process finished with exit code 1 -Thanks, Anirudh Tamsekar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] datetime substraction
Hi Alan/Danny/Steve, Thank you very much, I'm able to get the script working. -Regards, Anirudh Tamsekar On Tue, Aug 26, 2014 at 10:25 PM, Alan Gauld wrote: > On 26/08/14 10:10, Anirudh Tamsekar wrote: > > *Traceback (most recent call last):* >> * File "./rsyslog_check.py", line 22, in * >> *difft=cur_time-mt* >> *TypeError: unsupported operand type(s) for -: 'str' and 'str'* >> >> >> Request your help. Also point any good documentation where I can get >> quick reference. >> >> ct = time.ctime(os.path.getctime(tfile)) >> mt = time.ctime(os.path.getctime(tfile)) >> > > You are converting the times to strings before you subtract them. Don't do > that, subtract the times as returned by os.path.getctime() and time.time(). > > cur_time = time.ctime() >> difft=int(cur_time-mt) >> > > You need to subtract the numeric versions of the times not the strings. > > The best quick references are the dir() and help() builtin functions. > For more detail read the documentation for time, os.path (and maybe > datetime) modules. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] datetime substraction
Hi, I'm writing a script to monitor rsyslog (second level check). Overview: We have Monit for monitoring rsyslog, however we need a script to do a second level checks. I'm looking for a script which will check if rsyslog is being updated, also log rotation is happening. Incase of the new file created by logrotate, script should monitor the latest file. Also monitor last modified time and tail of the log. Filename Ex. 20140826_debug_log (datechanges with log rotate) In case of any issues such as file not being updated within the interval of 5 min, alert with disk status. I have written a script, however I'm not able to get the date substraction math right, getting the following error (Searched google and other resources too). *Traceback (most recent call last):* * File "./rsyslog_check.py", line 22, in * *difft=cur_time-mt* *TypeError: unsupported operand type(s) for -: 'str' and 'str'* Request your help. Also point any good documentation where I can get quick reference. ## #! /usr/bin/python # import libraries import os import sys import datetime import time import smtplib import Queue import threading import subprocess # Defining main variables tfile='/local/rsyslog/20140727_msg_debug.log' dt = datetime.datetime.now().strftime("%Y%m%d") file = "_msg_debug.log" filename = "%s%s" % (dt,file) filepath = '/local/rsyslog/'+filename ct = time.ctime(os.path.getctime(tfile)) mt = time.ctime(os.path.getctime(tfile)) cur_time = time.ctime() difft=int(cur_time-mt) tdelta=datetime.timedelta(minute=5) # Main if os.path.isfile(filepath) == True: print "%s exists \n" % (tfile) print "This file was created at %s UTC \n" % (ct) * if difft > tdelta:* *print "File is not modified, last modified at %s UTC" % (mt)* *else:* *print "File is being modified"* *else:* *os.path.isfile(filepath)* # Tailing the file tailq = Queue.Queue(maxsize=10) # buffer at most 100 lines def tail_forever(filepath): p = subprocess.Popen(["tail", tfile], stdout=subprocess.PIPE) while 1: line = p.stdout.readline() tailq.put(line) if not line: break threading.Thread(target=tail_forever, args=(tfile,)).start() print "\n # Tailing Log file %s" % (tfile) print "\n ***" print tailq.get() # blocks print tailq.get_nowait() # throws Queue.Empty if there are no lines to read exit() -Thanks, Anirudh Tamsekar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor