From d4eccca9fbfe7c0eb267a013033a988ba54e1d4b Mon Sep 17 00:00:00 2001 From: zhaoqifa Date: Thu, 26 Nov 2015 11:55:31 +0800 Subject: [PATCH 1/2] use with to open and close a file --- python/hangover.py | 15 ++++----------- python/smack_my_bitch_up.py | 15 ++++----------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/python/hangover.py b/python/hangover.py index bf923d8..c9b63e1 100755 --- a/python/hangover.py +++ b/python/hangover.py @@ -42,16 +42,9 @@ client.messages.create( ) try: - f = open('logs/file.txt', 'a') -except IOError as e: - # dir & file don't exist; create them - os.mkdir('logs') - f = open('logs/file.txt', 'a') + if not os.path.exists('logs'): + os.mkdir('logs') + with open('logs/file.txt', 'a') as lh: + lh.write("Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") + "\n") except Exception as e: print e -else: - pass - -# log it -f.write("Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") + "\n") -f.close() diff --git a/python/smack_my_bitch_up.py b/python/smack_my_bitch_up.py index 398c881..32b209d 100755 --- a/python/smack_my_bitch_up.py +++ b/python/smack_my_bitch_up.py @@ -43,16 +43,9 @@ client.messages.create( ) try: - f = open('logs/file.txt', 'a') -except IOError as e: - # dir & file don't exist; create them - os.mkdir('logs') - f = open('logs/file.txt', 'a') + if not os.path.exists('logs'): + os.mkdir('logs') + with open('logs/file.txt', 'a') as lh: + lh.write("Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") + "\n") except Exception as e: print e -else: - pass - -# log it -f.write("Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") + "\n") -f.close() From 8c00bbac16aa694cd07f34f1afe26079cef3fe4a Mon Sep 17 00:00:00 2001 From: zhaoqifa Date: Thu, 26 Nov 2015 13:23:38 +0800 Subject: [PATCH 2/2] write log to stdout --- python/hangover.py | 8 +------- python/smack_my_bitch_up.py | 8 +------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/python/hangover.py b/python/hangover.py index c9b63e1..1daab65 100755 --- a/python/hangover.py +++ b/python/hangover.py @@ -41,10 +41,4 @@ client.messages.create( body="Gonna work from home. " + random.choice(excuses) ) -try: - if not os.path.exists('logs'): - os.mkdir('logs') - with open('logs/file.txt', 'a') as lh: - lh.write("Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") + "\n") -except Exception as e: - print e +print "Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") diff --git a/python/smack_my_bitch_up.py b/python/smack_my_bitch_up.py index 32b209d..0884416 100755 --- a/python/smack_my_bitch_up.py +++ b/python/smack_my_bitch_up.py @@ -42,10 +42,4 @@ client.messages.create( body="Late at work. " + random.choice(reasons) ) -try: - if not os.path.exists('logs'): - os.mkdir('logs') - with open('logs/file.txt', 'a') as lh: - lh.write("Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") + "\n") -except Exception as e: - print e +print "Message sent at " + strftime("%a, %d %b %Y %H:%M:%S")