Skip to content

Instantly share code, notes, and snippets.

@tomatolog
Last active February 24, 2018 10:37
Show Gist options
  • Save tomatolog/9d5eada1f42fc714982c652f6fe9ea74 to your computer and use it in GitHub Desktop.
Save tomatolog/9d5eada1f42fc714982c652f6fe9ea74 to your computer and use it in GitHub Desktop.
add file with next number
import os, sys, re
def die(s):
print s
sys.exit ( 1 )
invoice_name = "invoice_"
got_files = []
cwd = os.getcwd()
for root, dirs, files in os.walk(cwd):
for name in files:
m = re.match("invoice_(\d*).*", name)
if m and len(m.group(1)):
n = os.path.splitext(name)[0]
got_files.append((name, int(m.group(1))))
if len(got_files)==0:
last_file = ('empty', 0)
else:
last_file = max(got_files, key=lambda x: x[1])
print("last file: %s" % last_file[0])
name = "%s%s.txt" % (invoice_name, last_file[1]+1)
print("file: %s" % (name))
fp = open(name, "w")
fp.write("Hi ,\n")
fp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment