-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28_Text_Files.py
More file actions
58 lines (50 loc) · 1.22 KB
/
Copy path28_Text_Files.py
File metadata and controls
58 lines (50 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Two types of files:
1. Text Files
-> Plain Text
-> XML
-> JSON
-> Source Code
2. binary Files
-> Compiled Code
-> App data
-> Media files
~ images
~ audio
~ video
"""
# open() can be used for opening the function
# creating a text file
f = open("28_guido_bio.txt")
text = f.read()
f.close()
print(text)
# Another method
# This method automatically closes the file
with open("28_guido_bio.txt") as fobj:
bio = fobj.read()
print(bio)
print("@"*80)
# Trying to open a file which is not available
try:
with open("future_lottery_numbers.txt") as f:
text2 = f.read()
except FileNotFoundError:
text2 = None
print(text2)
print("^"*80)
# Writing in a file
oceans = ["Pacific", "Atlantic", "Indian", "Southern", "Arctic"]
with open("28_oceans.txt", "w") as f:
for ocean in oceans:
f.write(ocean)
f.write("\n")
# Writing in a file
oceans = ["Pacific", "Atlantic", "Indian", "Southern", "Arctic"]
with open("28_oceans_2.txt", "w") as f:
for ocean in oceans:
print(ocean, file = f)
# To avoid overriding the file open file in append mode
with open("28_oceans_2.txt", "a") as ff:
print("="*23, file = ff)
print("These are 5 Oceans.", file = ff)