2
0
mirror of https://github.com/narkoz/hacker-scripts synced 2025-08-26 12:27:13 +00:00

use with to open and close a file

This commit is contained in:
zhaoqifa 2015-11-26 11:55:31 +08:00
parent f9321f0f07
commit d4eccca9fb
2 changed files with 8 additions and 22 deletions

View File

@ -42,16 +42,9 @@ client.messages.create(
) )
try: try:
f = open('logs/file.txt', 'a') if not os.path.exists('logs'):
except IOError as e: os.mkdir('logs')
# dir & file don't exist; create them with open('logs/file.txt', 'a') as lh:
os.mkdir('logs') lh.write("Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") + "\n")
f = open('logs/file.txt', 'a')
except Exception as e: except Exception as e:
print e print e
else:
pass
# log it
f.write("Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") + "\n")
f.close()

View File

@ -43,16 +43,9 @@ client.messages.create(
) )
try: try:
f = open('logs/file.txt', 'a') if not os.path.exists('logs'):
except IOError as e: os.mkdir('logs')
# dir & file don't exist; create them with open('logs/file.txt', 'a') as lh:
os.mkdir('logs') lh.write("Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") + "\n")
f = open('logs/file.txt', 'a')
except Exception as e: except Exception as e:
print e print e
else:
pass
# log it
f.write("Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") + "\n")
f.close()