Skip to content

Instantly share code, notes, and snippets.

@tadone
Created October 12, 2017 12:48
Show Gist options
  • Save tadone/62aad10f0f789ace9eaa0ef669a599a5 to your computer and use it in GitHub Desktop.
Save tadone/62aad10f0f789ace9eaa0ef669a599a5 to your computer and use it in GitHub Desktop.
Read/Write to/from file
# Wrtie to file (w write)
f = open('helloworld.txt','w')
f.write('hello world')
f.close()
# Write to BINARY file (wb write binary)
f = open('database_file','wb')
f.write('hello world')
f.close()
# Append to file (a append)
f = open('helloworld.txt','a')
f.write('\n' + 'hello world')
f.close()
# Open file (r read)
f = open('helloworld.txt','r')
message = f.read()
print(message)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment