You are on page 1of 3

FILE HANDLING

TEXT FILE BINARY FILE CSV FILE


MODULE - pickle csv
FILE MODES r,w,a(read,write,append only) rb,wb,ab(read,write,append only) r,w,a(read,write,append
r+,w+,a+ (read and write, write rb+ (or) r+b ,wb+ (or) w+b ,ab+ only)
and read, write and read) (or) a+b (read and write, write and r+,w+,a+ (read and write,
write and read, write
read, write and read) and read)
Function –  read() = to read the entire [try except block is mandatory to avoid  reader()= it parses
reading a document FileNotFound error or EOF error”] the delimited csv
file filehandle/fileobject.read() [with open statement can also be used file data and loads
f.read() to avoid FileNotFound error or EOF it into an iterable.
 read(n)= to read the error and also it will close the file readerobject=csv.reader(
specified number of bytes automatically not needed to include file handle / file object)
from the file close()] cr=csv.reader(f)
filehandle/fileobject.read(n) (reading / unpickling)
f.read(5)  load()
 readline()= to read a single object=pickle.load(filehandle/fileobject
line from the file )
filehandle/fileobject.readline() g=pickle.load(f)
f.readline()
 readlines()= to read all the
lines in a file but displays the
output in the form of list.
filehandle/fileobject.readlines()
f.readlines()
Function-  write()= to write the strings (WRITING / PICKLING)  writer()=it helps to
wring data in to the file  dump()= to write data in to a write data in to
in to the filehandle/fileobject.write(variabl binary file the csv file.
files e name) pickle.dump(variable name,fileobject/ And also it is
f.write(str) filehandle) useful convert the
 writelines()= to write the pickle.dump(num,f) data in to csv
strings in the form of list in writable form(i.e.
to the file and also note that delimited string
newline has to coded by us format)
while using writelines. writerobject=csv.writer(f
filehandle/fileobject.writelines(var ile handle / file object)
iable name) cw=csv.writer(f)
f.writelines(str)

Additional -  tell() = it returns an integer which  writerow()= it is


function specifies the current position of used to write a
used the filepointer in a file . single row data in
filehandle/fileobject.tell() to a file
f.tell() writerobject.writerow(
variablename/ data)
 seek()= it whelps to move the EXAMPLE1:
filepointer to the desired position cw.writerow(rec)
inside the file. EXAMPLE 2:
filehandle/fileobject.seek(offset, cw.writerow([“rollno”,
mode/ reference point) ”marks”])
MODES: NOTE: writerow()
0= beginning of the file function can be used
1=current position of the file inside the for loop to
2=end of the file fetch multiple data from
EXAMPLE: the user)
f.seek(5,0)
the above example will move the file  writerows()= it is
pointer to the 5th byte position from used to write
the beginning of a file multiple data in to
the file.
writerobject.writerows(
variablename/ data)
EXAMPLE1:
rec=[4,5,6,7,88,(56),j,m,9
00,567]
cw.writerows(rec)
EXAMPLE 2:
cw.writerows([“rollno”],
[”marks”],[“centum”])

You might also like