Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbd28590

Browse files
committed
Add file read write demo (I/O)
1 parent4de37b9 commitbd28590

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_propertyname="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
4+
<pydev_propertyname="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.6</pydev_property>
5+
<pydev_pathpropertyname="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+
defreadfiledemo():
12+
f=open('smallfile.txt')
13+
forlineinf:
14+
print(line,end='')
15+
16+
defreadwritefiledemo():
17+
fin=open('smallfile.txt','r')
18+
fout=open('demofile1.txt','w')
19+
forlineinfin:
20+
print(line,file=fout,end='')
21+
22+
defbigfiledemo():
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+
whilelen(buffer):
31+
fout.write(buffer)
32+
print('.',end='')
33+
buffer=fin.read(buffersize)
34+
35+
print('\nDone writing the big file.')
36+
37+
defreadwritebinaryfilesdemo():
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+
whilelen(buffer):
45+
fout.write(buffer)
46+
print('.',end='')
47+
buffer=fin.read(buffersize)
48+
print('\nDone writing the image.')
49+
50+
defmain():
51+
#writebigfile()
52+
readwritebinaryfilesdemo()
53+
readfiledemo()
54+
readwritefiledemo()
55+
bigfiledemo()
56+
57+
58+
59+
defwritebigfile():
60+
f=open('largefile.txt','w')
61+
foriinrange(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)

[8]ページ先頭

©2009-2025 Movatter.jp