Basic Functions used in Text and Binary Files, Schemes and Mind Maps of Computer science

Python functions of text and binary files

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 11/19/2023

nivedithaa-sam
nivedithaa-sam 🇮🇳

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Functions of Text file:
1. read() :
Reads the entire file.
Read(n) reads n bytes of character from the file.
It returns the read bytes in the form of a string
Syntax: <variable>=<file handle>.read([n])
Eg: s=f.read()
2. readline():
Reads a line at a time.
Readline(n) reads a line at most n bytes
It returns the read bytes in the form of a string
Syntax: <variable>=<file handle>.readline([n])
Eg: s=f.readline()
3.readlines():
Reads all lines of a text file and returns them in a list.
Syntax: <variable>=<file handle>.readlines()
Eg: s=f.readlines()
4. write():
Writes the string given in the argument to the file referenced by <filehandle>
Syntax: <file handle>.write(str)
Eg: f.write(‘This is my file’)
5. writelines():
Writes all strings of the list given in the argument to the file referenced by
<filehandle>
Syntax: <file handle>.writelines(list)
Eg: f.writelines([‘This is my file’, ‘I am writing text file’])
6. flush():
To force Python, to write the contents of the buffer onto storage, use flush()
function.
When the function close() is called implicitly, Python will flush the files
automatically. But the flush() function will flush the data before closing the file.
Syntax: <file handle>.flush()
Eg: f.flush()
Functions of Binary file:
pf2

Partial preview of the text

Download Basic Functions used in Text and Binary Files and more Schemes and Mind Maps Computer science in PDF only on Docsity!

Functions of Text file:

  1. read() : ● Reads the entire file. ● Read(n) reads n bytes of character from the file. ● It returns the read bytes in the form of a string Syntax: =.read([n]) Eg: s=f.read()
  2. readline(): ● Reads a line at a time. ● Readline(n) reads a line at most n bytes ● It returns the read bytes in the form of a string Syntax: =.readline([n]) Eg: s=f.readline() 3.readlines(): ● Reads all lines of a text file and returns them in a list. Syntax: =.readlines() Eg: s=f.readlines()
  3. write(): ● Writes the string given in the argument to the file referenced by Syntax: .write(str) Eg: f.write(‘This is my file’)
  4. writelines(): ● Writes all strings of the list given in the argument to the file referenced by Syntax: .writelines(list) Eg: f.writelines([‘This is my file’, ‘I am writing text file’])
  5. flush(): ● To force Python, to write the contents of the buffer onto storage, use flush() function. ● When the function close() is called implicitly, Python will flush the files automatically. But the flush() function will flush the data before closing the file. Syntax: .flush() Eg: f.flush()

Functions of Binary file:

  1. dump(): ● In order to write a binary file, the dump() function of the pickle module is used. ● It will perform pickling where the object is converted to bytes of stream. Syntax: pickle.dump(, <file-handle>) Eg: pickle.dump(d,f)
  2. load(): ● In order to read data from a binary file, the load() function of the pickle module is used. ● It will perform unpickling where bytes of the stream is converted to an object. Syntax: =pickle.load(<file-handle>) Eg: d=pickle.load(f)
  3. tell(): ● It returns the current position of the file pointer in the file. Syntax: =<file-handle>.tell() Eg: pos=f.tell()
  4. seek(): ● It changes the file pointer's position by placing it at the specified position in the opened file. Syntax: <file-handle>.seek(offset [,mode]) Where an offset is a number specifying the number of bytes. mode is a number 0,1 or 2 0 – to move file pointer w.r.t. the beginning of the file (default mode) 1 – to move file pointer w.r.t. the current position of the file 2 – to move file pointer w.r.t. the end of the file Eg: f.seek(30,0)