-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfileOperations
More file actions
23 lines (16 loc) · 776 Bytes
/
fileOperations
File metadata and controls
23 lines (16 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-Files :
We can create a file in 3 different modes.
1.Write
2.Read
3.Append
Write : If the file exists then it will overwrite, otherwise it will create a new file and add the contents
Example:
with open("firstfile.txt", 'w') as fh:
fh.write(f"Hello, Good Morning \ni am writing the second line \n")
Read : Here the file will be opened only for reading purpose. We cannot modify the file contents.
Append : If the file exists it will add the contents at the end of the file, if the file does not exist then it will create a new file and add the contents
Example :
fruit_list=["Apple","Orange","Banana"]
with open("firstfile.txt", 'a') as fh:
for fruit in fruit_list:
fh.write(f"{fruit} \n") #\n to move the filepointer to new line