-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_fileops.py
More file actions
32 lines (24 loc) · 1.01 KB
/
python_fileops.py
File metadata and controls
32 lines (24 loc) · 1.01 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
# We will learn about opening and editing files from python program
# open file using open function
# other argument to open function specifies mode, r = reading, r+ = read and write, a = append to file and so on.
# The other way we can open file is to use context
# the advantage of using context is that we don't need to close the file, it will take care of it
# f_handle = open('test.txt', 'r')
#with open('test.txt', 'r') as f_handle:
#print(f_handle.read())
# Instead of reading whole file contents using read we can read line by line using readlines or readline functions
#f_contents = f_handle.readlines()
#print(f_contents, end='')
#f_contents = f_handle.readline()
#print(f_contents, end='')
#for line in f_handle:
# print(line, end='')
# We are using write function to create copy of the existing file
with open('test.txt', 'r') as rf:
with open('test_copy.txt', 'w') as wf:
for line in rf:
wf.write(line)
# print(f_handle.name)
# print(f_handle.mode)
# print(f_handle.read())
# f_handle.close()