top of page


Write to a File with Python
Unit 2 Summary - part 2

How to create a TXT file with Python


Writing to a file coding:

-
Line 2 and 8: message is a variable to add a text.​
​​
-
Line 4's (filename, "w") is used to OVERWRITE a file.
​​
-
Line 5, 6, 7: /n is used to create a new line​


-
Line 2 & 5: header is a variable to add a text.
​​
-
Line 4's (filename, "a") is used if you don't want to overwrite, you just want to add something to the end of the file.​​​


-
Line 2 & 3 are used to let users input their own item to the text.


-
Line 2 is used to add a list in the file.
​​
-
Line 5 is to add the list with for loop.

How to read a TXT file with Python



Read a TXT file coding:

-
Line 3 is used to open a file on Python
​​
-
Line 5 is used to display the list with for loop.

bottom of page
