Skip to content

Instantly share code, notes, and snippets.

@Robofied
Created February 4, 2019 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robofied/05cc0439dcd2c2c2fab948ed2efe2fcb to your computer and use it in GitHub Desktop.
Save Robofied/05cc0439dcd2c2c2fab948ed2efe2fcb to your computer and use it in GitHub Desktop.
## If file is not created then we can also create it like that.
## It will automatically generate new file in the current directory.
with open('file2.txt','w') as f1:
pass
#If we will go to our file then it will be empty right now.
## Writing to the created file.
with open('file2.txt','w') as f1:
f1.write("Hello")
#If we will go to the file2.txt then it will write Hello in that file.
## Again writing then it is remove the previous contents. So in order to avoid the removal of previous content.
## One can use the append mode, it will append the data in already existing file.
with open('file2.txt','w') as f1:
f1.write("Test1")
#If we will go to the file2.txt now then it will write Test1 and remove the previous written content i.e, Hello.
## Appending in files
## using append('a') mode, it will write to existing file without removing previous content.
with open('file2.txt','a') as f1:
f1.write("Test2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment