In Python mainly basic file operations are
-reading
-writing
-appending
and more on file pointers
so let us see one by one
first create text file in python ide
test.txt
======
my name is deepak
File Operations:Reading
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> f=open("test.txt","rb")
>>> print f
<open file 'test.txt', mode 'rb' at 0x029F6078>
>>> f.read()
'\x00\x00\x00\x00\x00my name is deepak'
>>> f.close()
>>> print f
<closed file 'test.txt', mode 'rb' at 0x029F6078>
>>>
let us understand deeply
--------------------------
so here we create text file and in that i wrote "my name is deepak"
so when i type
f=open("test.txt","rb")
so what is the syntax here
i am take f as variable and for that f variable i am giving one file operation
ie. with open() function
so the open() syntax will be
open(file name, mode)
so we understand file name that is text.txt
but what is rb
let us go and understand what is rb
Modes:
=====
rb+
Opens the file for reading and writing. File pointer will be at the beginning of the file.
wb+
Opens for reading and writing. Overwrites the existing file if the file exists, otherwise a new file is created.
ab+
Opens the file for appending and reading. The file pointer is at the end of the file if the file exists, otherwise a new file is created for reading and writing
ok we understand what is mode ok we will see the next level
ie.,
File operations: Writing>>> f=open("test.txt","wb")
>>> print f
<open file 'test.txt', mode 'wb' at 0x02AE35F8>
>>> f.write("hai this is deepak")
>>> f.write("and this is updated one")
>>> f.close()
<built-in method close of file object at 0x02AE35F8>
>>>
File Operation : Appending
>>> f = open("test.txt","ab")
>>> print f
<open file 'test.txt', mode 'ab' at 0x02AE35F8>
>>> f.write("hello world, again")
>>> f.close()
>>> f = open("test.txt","rb")
>>> f.read()
'hai this is deepakhello world, again'
====================
More
f.tell()
gives current position within file f
f.seek(x[, from])
change file pointer position within
file f, where
from = 0 from beginning of file
from = 1 from current position
from = 2 from end of file
press clt+f6 then the ide of python will restart
>>> ================================ RESTART ========================
>>> f = open("test.txt","rb")
>>> f.read()
'hai this is deepakhello world, again'
>>> f.readline()
''
>>> f.tell()
36L
>>>
>>> f.seek(5)
>>> f.tell()
5L
-reading
-writing
-appending
and more on file pointers
so let us see one by one
first create text file in python ide
test.txt
======
my name is deepak
File Operations:Reading
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> f=open("test.txt","rb")
>>> print f
<open file 'test.txt', mode 'rb' at 0x029F6078>
>>> f.read()
'\x00\x00\x00\x00\x00my name is deepak'
>>> f.close()
>>> print f
<closed file 'test.txt', mode 'rb' at 0x029F6078>
>>>
let us understand deeply
--------------------------
so here we create text file and in that i wrote "my name is deepak"
so when i type
f=open("test.txt","rb")
so what is the syntax here
i am take f as variable and for that f variable i am giving one file operation
ie. with open() function
so the open() syntax will be
open(file name, mode)
so we understand file name that is text.txt
but what is rb
let us go and understand what is rb
Modes:
=====
rb+
Opens the file for reading and writing. File pointer will be at the beginning of the file.
wb+
Opens for reading and writing. Overwrites the existing file if the file exists, otherwise a new file is created.
ab+
Opens the file for appending and reading. The file pointer is at the end of the file if the file exists, otherwise a new file is created for reading and writing
ok we understand what is mode ok we will see the next level
ie.,
File operations: Writing>>> f=open("test.txt","wb")
>>> print f
<open file 'test.txt', mode 'wb' at 0x02AE35F8>
>>> f.write("hai this is deepak")
>>> f.write("and this is updated one")
>>> f.close()
<built-in method close of file object at 0x02AE35F8>
>>>
File Operation : Appending
>>> f = open("test.txt","ab")
>>> print f
<open file 'test.txt', mode 'ab' at 0x02AE35F8>
>>> f.write("hello world, again")
>>> f.close()
>>> f = open("test.txt","rb")
>>> f.read()
'hai this is deepakhello world, again'
====================
More
f.tell()
gives current position within file f
f.seek(x[, from])
change file pointer position within
file f, where
from = 0 from beginning of file
from = 1 from current position
from = 2 from end of file
press clt+f6 then the ide of python will restart
>>> ================================ RESTART ========================
>>> f = open("test.txt","rb")
>>> f.read()
'hai this is deepakhello world, again'
>>> f.readline()
''
>>> f.tell()
36L
>>>
>>> f.seek(5)
>>> f.tell()
5L
No comments:
Post a Comment