2
0
mirror of https://github.com/narkoz/hacker-scripts synced 2025-08-25 11:57:24 +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:
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()

View File

@ -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()