Skip to content

Instantly share code, notes, and snippets.

@Tharwat96
Last active March 9, 2020 22:28
Show Gist options
  • Save Tharwat96/11beafee94c465cb1ab6a2b2ae053288 to your computer and use it in GitHub Desktop.
Save Tharwat96/11beafee94c465cb1ab6a2b2ae053288 to your computer and use it in GitHub Desktop.
# write/overwrite file
f = open("text.txt", "w")
f.write("bla bla")
f.close()
# search for something in file
with open('data') as f:
if 'test' in f.read():
print("true")
f.close()
# storing and restoring dictionaries
import json
with open('data.json', 'w') as fp:
json.dump(data, fp)
# Supply extra arguments like sort_keys or indent to get a pretty result. The argument sort_keys will sort the keys alphabetically and indent will indent your data structure with indent=N spaces.
json.dump(data, fp, sort_keys=True, indent=4)
JSON load:
with open('data.json', 'r') as fp:
data = json.load(fp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment