-
Notifications
You must be signed in to change notification settings - Fork 0
/
n
executable file
·111 lines (97 loc) · 2.98 KB
/
n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# Author: Nishad Trivedi
# TODO
# Use file w/out python in front [x]
# Create/write custom file [x]
# Read custom file [x]
# Delete custom file []
# Delete file [x]
# implement colors?? []
# NOTES: http://stackoverflow.com/questions/1466000/python-open-built-in-function-difference-between-modes-a-a-w-w-and-r
import sys
import os
def main():
if len(sys.argv) < 2:
help()
elif sys.argv[1] in ["-h", "help"]:
help()
elif sys.argv[1] in ["-v", "version"]:
version()
elif sys.argv[1] in ["-r", "read"]:
read()
elif sys.argv[1] in ["-d", "delete"]:
delete()
else:
write()
def write():
if sys.argv[1].endswith('.txt'):
file_name = sys.argv[1]
notes = sys.argv[2:]
with open('{0}'.format(file_name), 'a+') as f:
line = ' '.join(notes)
f.write('%s\n' % line)
f.close()
else:
notes = sys.argv[1:]
with open('notes.txt', 'a+') as f:
line = ' '.join(notes)
f.write("%s\n" % line)
f.close()
def read():
try:
if len(sys.argv) == 3: # Implement this way by checking length!!!
file_name = sys.argv[2]
with open('{0}'.format(file_name), 'r') as f:
print
print f.read()
else:
with open('notes.txt', 'r') as f:
print
print f.read()
except IOError:
file_name = sys.argv[2]
if file_name.endswith('txt'):
print "%s hasn't been created yet. Should I create it? [y/n]" % file_name[:-4] #sub file_name for file
create()
else:
print "Your notes don't exist. Should I create them? [y/n]"
create()
def create():
prompt = '> '
print prompt,
answer = raw_input().lower()
if answer in ["yes", "y"]:
with open('notes.txt', 'a+') as f:
print "Write something otherwise press 'n'"
print prompt,
notes = raw_input()
if notes in ["no", "n"]:
pass
else:
f.write("%s\n" % notes)
f.close()
def version():
version = 0.3
print "Using v%s" % version
def help():
print """\n
n [file] [notes] Writes notes into file.txt (notes.txt by default)
n [-r, read] [file] Read notes from file (notes.txt by default)
n [-d, delete] [file] Deletes file (notes.txt by default)
n [-h, help] Opens help menu
n [-v, version] Version number
\n
"""
def delete():
if len(sys.argv) == 3:
try:
os.remove("%s" % sys.argv[2])
except OSError:
print "%s doesn't exist!" % sys.argv[2][:-4]
else:
try:
os.remove("notes.txt")
except OSError:
pass
if __name__ == "__main__":
main()