-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReading and Writing Files.py
66 lines (38 loc) · 1.14 KB
/
Reading and Writing Files.py
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
59
60
61
62
63
64
65
#!/usr/bin/env python
# coding: utf-8
# # Read a file
# In[49]:
# Open the file "hello.txt" in read mode
f = open("hello.txt", "r")
# Read the contents of the file
file_data = f.read()
# Close the file to free up system resources
f.close()
# Print the contents of the file
print(file_data)
# # too many files open
# In[57]:
# Initialize an empty list to store file objects
files = []
# Loop 10,000 times
for i in range(10000):
# Open the file "hello.txt" in read mode and append the file object to the list
files.append(open("hello.txt", "r"))
# Print the current iteration number
print(i)
# # Creating new file thru Python Code
# In[53]:
# Open the file "another_file.txt" in write mode
f = open("another_file.txt", "w")
# Write the string 'Hello World!' to the file
f.write('Hello World!')
# Close the file to save the changes
f.close()
# # auto close the file without using f.close()
# In[55]:
# Open the file "another_file.txt" in read mode using a context manager
with open("another_file.txt", "r") as f:
# Read the contents of the file
file_data = f.read()
# Print the contents of the file
print(file_data)