Skip to content

Commit bd28590

Browse files
committed
Add file read write demo (I/O)
1 parent 4de37b9 commit bd28590

File tree

6 files changed

+10095
-0
lines changed

6 files changed

+10095
-0
lines changed

FilesIO/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>FilesIO</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>

FilesIO/.pydevproject

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?><pydev_project>
3+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
4+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.6</pydev_property>
5+
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
6+
<path>/${PROJECT_DIR_NAME}/src</path>
7+
</pydev_pathproperty>
8+
</pydev_project>

FilesIO/src/ants.jpg

53.2 KB
Loading

FilesIO/src/filesio.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'''
2+
Created on Aug 8, 2017
3+
4+
@author: Aditya
5+
6+
This program demonstrates the python IO capabilities.
7+
8+
File reading and writing for text and binary files are demonstrated.
9+
'''
10+
11+
def readfiledemo():
12+
f = open('smallfile.txt')
13+
for line in f:
14+
print(line, end='')
15+
16+
def readwritefiledemo():
17+
fin = open('smallfile.txt', 'r')
18+
fout = open('demofile1.txt', 'w')
19+
for line in fin:
20+
print(line, file = fout, end = '')
21+
22+
def bigfiledemo():
23+
fin= open('largefile.txt','r')
24+
fout = open('demofile2.txt','w') # open demofile to wite
25+
buffersize = 50000 # buffer size in bytes
26+
27+
buffer = fin.read(buffersize)
28+
29+
print('Read method on file handle object is not iterable. Therefore, using while-loop and not for-loop.')
30+
while len(buffer):
31+
fout.write(buffer)
32+
print('.', end='')
33+
buffer = fin.read(buffersize)
34+
35+
print('\nDone writing the big file.')
36+
37+
def readwritebinaryfilesdemo():
38+
fin = open('ants.jpg','rb') # read binary
39+
fout = open('demo.jpg','wb') # write binary
40+
buffersize = 50000 # buffer in bytes
41+
buffer = fin.read(buffersize)
42+
print('Note: Image read in binary format.')
43+
print(buffer)
44+
while len(buffer):
45+
fout.write(buffer)
46+
print('.',end='')
47+
buffer = fin.read(buffersize)
48+
print('\nDone writing the image.')
49+
50+
def main():
51+
#writebigfile()
52+
readwritebinaryfilesdemo()
53+
readfiledemo()
54+
readwritefiledemo()
55+
bigfiledemo()
56+
57+
58+
59+
def writebigfile():
60+
f = open('largefile.txt', 'w')
61+
for i in range(10000):
62+
print('{:05} Yo the big file of the Files'.format(i), file=f)
63+
print('Done writing.')
64+
65+
if __name__ == '__main__':main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy