Skip to content

Instantly share code, notes, and snippets.

@vemacs
Created May 1, 2016 17:38
Show Gist options
  • Save vemacs/4ad2b7e0e29695b52b0e432bc4647882 to your computer and use it in GitHub Desktop.
Save vemacs/4ad2b7e0e29695b52b0e432bc4647882 to your computer and use it in GitHub Desktop.
import os, yaml
datadir = 'userdata'
key = 'money'
maxbal = 2500.0
def is_non_zero_file(fpath):
return True if os.path.isfile(fpath) and os.path.getsize(fpath) > 0 else False
for f in os.listdir(datadir):
if str(f).endswith('.yml'):
filepath = os.path.join(datadir, f)
should_correct = False
with open(filepath, 'r') as stream:
if not is_non_zero_file(filepath):
continue
userfile = yaml.load(stream)
if key in userfile and float(userfile[key]) > maxbal:
should_correct = True
if should_correct:
with open(filepath, 'w') as stream:
print('Corrected ' + filepath + ' with balance ' + userfile[key])
userfile[key] = '0.0'
stream.write(yaml.dump(userfile, default_flow_style=False))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment