-
Notifications
You must be signed in to change notification settings - Fork 6
/
python15 (binary & appending).py
54 lines (47 loc) · 1.65 KB
/
python15 (binary & appending).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
# extra
# binary file
# birary file means photos, videos etc.
# it is calculated in bytes.
# the mode of binary file is "b"
# copying a binary file
with open("boss.jpg","rb") as rf:
file_contents=rf.read()
with open("boss_copy.jpg","wb") as wf:
wf.write(file_contents)
# or-
with open("boss.jpg","rb") as rf:
with open("boss_copy.jpg","wb") as wf:
for line in rf:
wf.write(line)
# we can do this by chunc tecnic also.
with open("boss.jpg","rb") as rf:
with open("boss_copy.jpg","wb") as wf:
chunk_size=4096
rf_chunk=rf.read(chunk_size)
while len(rf_chunk) >0:
wf.write(rf_chunk)
rf_chunk=rf.read(chunk_size)
# appending to a file
# if a file dont exists and we write a file it will be automatically created.
# if a file exists and we write a file, it will delete previous contents and overwrite it.
# if we dont want to remove old contents of a existed file, we can use append mode.
filename="UserGuest.txt"
accessmode="a"
with open(filename, accessmode) as myfile:
myfile.write("I am shawki")
# different files with their extensions
# text document - .txt
# excel (comma separated) - .csv
# excel - .xlxs
# command prompt - .cmd
# ms word - .docx
# photo - .jpg
# audio - .mp3
# video - .mp4
# python - .py
# powerpoint - .pptx
# zip - .zip
# pdf file - .pdf
# subtitle - .str
# torrent file - .torrent
# application - .exe