-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch_10_file_handling.py
46 lines (36 loc) · 924 Bytes
/
ch_10_file_handling.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
#!/usr/bin/python
#AUTHOR: alexxa
#DATE: 18.12.2013
#SOURSE: https://www.skillfeed.com/courses/539-python-for-beginners
#PURPOSE: File Handling
# opening file
file = open('text.txt')
for line in file:
print(line, end = '')
# writing file
input1 = open('text.txt', 'r')
output1 = open('newfile.txt', 'w')
for line in input1:
print(line, file = output1, end = '')
print()
# handling big files
buffersize = 2
input2 = open('text.txt', 'r')
output2 = open('newbigdata.txt', 'w')
buffer = input2.read(buffersize)
bufferlimit = 40
while bufferlimit > 0:
output2.write(buffer)
print('.', end = '')
bufferlimit = bufferlimit - buffersize
print()
# binary files
buffersize = 10000
input3 = open('pic.jpg', 'rb')
output3 = open('newpic.jpg', 'wb')
buffer = input3.read(buffersize)
while len(buffer):
output3.write(buffer)
print('.', end = '')
buffer = input3.read(buffersize)
#END