Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions blah.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
I3
.
2 changes: 2 additions & 0 deletions blah2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
I2
.
12 changes: 10 additions & 2 deletions counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def update_counter(file_name, reset=False):

file_name: the file that stores the counter to be incremented. If the file
doesn't exist, a counter is created and initialized to 1.
reset: True if the counter in the file should be rest.
reset: True if the counter in the file should be reset.
returns: the new counter value

>>> update_counter('blah.txt',True)
Expand All @@ -29,7 +29,15 @@ def update_counter(file_name, reset=False):
>>> update_counter('blah2.txt')
2
"""
pass
f = open(file_name, 'r+')
if reset or not exists(file_name):
counter = 1
else:
counter = load(f) + 1
f.seek(0,0)
dump(counter, f)

return counter

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