Take notes on the command line: Difference between revisions
Content added Content deleted
m (formatting) |
(→{{header|Python}}: Solution.) |
||
Line 16: | Line 16: | ||
:done |
:done |
||
</lang> |
</lang> |
||
=={{header|Python}}== |
|||
<lang python>import sys, datetime |
|||
#sys.argv[1:] = 'go for it'.split() |
|||
if len(sys.argv) == 1: |
|||
try: |
|||
f = open('notes.txt', 'r') |
|||
sys.stdout.write(f.read()) |
|||
f.close() |
|||
except IOError: |
|||
pass |
|||
else: |
|||
f = open('notes.txt', 'a') |
|||
f.write(datetime.datetime.now().isoformat() + '\n') |
|||
f.write("\t%s\n" % ' '.join(sys.argv[1:])) |
|||
f.close()</lang> |
|||
'''Sample notes.txt file''' |
|||
After assorted runs: |
|||
<pre>2010-04-01T17:06:20.312000 |
|||
go for it |
|||
2010-04-01T17:08:20.718000 |
|||
go for it</pre> |
Revision as of 16:12, 1 April 2010
Take notes on the command line
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
Take notes on the command line is part of Short Circuit's Console Program Basics selection.
Take notes on command line
NOTES.CMD is a commandline tool written in DOS Batch language. The task is to implement the same in your language.
Invoking NOTES without commandline arguments displays the current contents of the local NOTES.TXT if it exists. If NOTES has arguments, the current date and time are appended to the local NOTES.TXT followed by a newline. Then all the arguments, joined with a space, and prepended with a tab, are written to NOTES.TXT
DOS Batch File
<lang dos> @echo off if %1@==@ ( if exist notes.txt more notes.txt goto done ) echo %date% %time%:>>notes.txt echo %*>>notes.txt
- done
</lang>
Python
<lang python>import sys, datetime
- sys.argv[1:] = 'go for it'.split()
if len(sys.argv) == 1:
try: f = open('notes.txt', 'r') sys.stdout.write(f.read()) f.close() except IOError: pass
else:
f = open('notes.txt', 'a') f.write(datetime.datetime.now().isoformat() + '\n') f.write("\t%s\n" % ' '.join(sys.argv[1:])) f.close()</lang>
Sample notes.txt file
After assorted runs:
2010-04-01T17:06:20.312000 go for it 2010-04-01T17:08:20.718000 go for it