The built-in Python method open() ( doc ) usually uses two arguments: the file path and . You have three main modes (most commonly used): r , w and a .
r means reading and will open("/path/to/myFile.txt", 'r') open an existing file and be able to read it (without editing) using myFile.readlines() or other methods that you can find in this documentation .
w means write and will not only erase all files (if they exist), but also allow you to write new things through myFile.write("stuff I want to write") .
a means adding and adding content to an existing file without deleting what might have been written on it. This is an argument to use when adding lines to non-empty files.
You should not forget to close the file after completing work with it using myFile.close() , because it is only at the moment when all changes, updates, records are completed.
A small snippet for adding a line:
f = open("/path/to/myFile.txt", 'a') f.write("This line will be appended at the end") f.close()
If the contents of the file were like
"Stuff Stuff which was created long ago"
Then the file after the code looks like
"Stuff Stuff which was created long ago This line will be appended at the end"
source share