Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion counter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#Victor Bianchi - Software Design - Fall 2017
#Toolbox Assignment: Pickling

""" A program that stores and updates a counter using a Python pickle file"""

from os.path import exists
Expand Down Expand Up @@ -30,7 +33,17 @@ def update_counter(file_name, reset=False):
>>> update_counter('blah2.txt')
2
"""
pass
if exists(file_name) and not reset:
f = open(file_name,'r+')
counter = int(f.read().strip('\n'))
counter += 1
else:
f = open(file_name,'w')
counter = 1

f.seek(0,0)
f.write(str(counter))
return counter

if __name__ == '__main__':
if len(sys.argv) < 2:
Expand Down