In [ ]:
# files
# Persistant way of storing a data.
# .txt,.xls,.html
# file only does three things - read,write,append
# can i insert in between line ? - NO
# can i insert in between words ? - NO
# if you want to do above activities like writing in between line or a word you need to switch to a editor.
In [ ]:
# modes
# r - read - in this mode you can only read.
# w - write - write into the file.
# you can write into a file. If your file is not there it gets created. If it exists it get truncated to zero.
# a - append - appending to the file.
# you can keep appending to a file. adding lines to the end of the file.
#
# take example of a wordpad and a notepad. - encoding
# b - binary mode - will take take care of this encoding.
# rb,wb,ab
In [8]:
# opening a file
# f is a variable,we call a file handle. you can put any name to this.
# open is a function which is used for opening the file.
# it takes filename and mode as arguments.
# file.txt is the filename,since we opened the ipython or ipython notebook in same location you dont need to give the path.
# say: your file is in /tmp/file1.txt locations,so you can do the following.
# g = open('/tmp/file.txt','r')
f = open('file.txt','r')
# or
# f = open('file1.txt')
# if you dont specify the mode, which means you are opening the file in "r" mode automatically.
In [3]:
print dir(f)
In [4]:
print f.mode
In [5]:
print f.name
In [7]:
# f.read
print help(f.read)
In [9]:
print f.read(2)
In [10]:
print f.read(2)
In [11]:
print f.read()
In [12]:
print f.read()
In [ ]:
# we did not receive anything.we reached the end of the file. the pointer is stuck at the end. so you dont get to see anything.
In [15]:
# f.tell
print help(f.tell)
In [16]:
print f.tell()
In [17]:
# f.seek
In [18]:
print help(f.seek)
In [19]:
print f.seek(0)
In [20]:
print f.tell()
In [21]:
# now lets do a f.read and save into a variable called my_string
my_string = f.read()
print my_string,type(my_string)
In [22]:
# f.readline
print help(f.readline)
In [23]:
print f.tell()
In [24]:
# bringing back the pointer to zero position
f.seek(0)
In [25]:
print f.tell()
In [26]:
print f.readline()
In [27]:
print f.readline()
In [28]:
print f.readline()
In [29]:
print f.readline()
In [30]:
print f.readline()
In [31]:
print f.readline() # its not printing anything as we reached the end of the file.
In [32]:
f.readline()
Out[32]:
In [33]:
# f.readlines
print help(f.readlines)
In [34]:
f.seek(0) # making sure my pointer is set to zero
In [35]:
my_lines = f.readlines()
In [36]:
print my_lines,type(my_lines)
In [37]:
# f.readinto
print help(f.readinto)
In [38]:
# f.xreadlines
print help(f.xreadlines)
In [39]:
# snapshot
# open,name,mode,file modes,tell,seek,read,readline,readlines,readinto,xreadlines
In [ ]:
# write
In [40]:
g = open("newfile.txt",'w')
# g is a new file handle
# opened in mode w and having the file name as newfile.txt
In [41]:
print help(g.write)
In [42]:
# you entered over keyboard,
g.write("This is my first line.\nThis is my second line.\nThis is my third line.\nThis is my fourth line.\n")
In [43]:
# after doing this i dont see any content in the file.
# the content is till int he buffer
# I/O devices
# keyboard -> I/O buffer/cache -> cpu -> I/O buffer/cache -> disk
# cpu is faster than your input/output devices
In [44]:
# g.flush or g.close
print help(g.flush)
In [45]:
print help(g.close)
In [46]:
g.flush()
# this will flush the I/O buffers and dump it on the device(for our case its a disk)
# Also your file is open for further writes too.
In [48]:
# what if you close the file.
g.close()
# it closes the file and you cannot do anymore operation on top of the file.
In [49]:
print g.read()
In [51]:
# attritute f.closed set to True
print g.closed # your file is set to true,as it is closed.
print f.closed # your file is set to false,as it is opened.
In [ ]: