File handling in Python - PowerPoint PPT Presentation

About This Presentation
Title:

File handling in Python

Description:

Python handles files differently as text or binary, and this is important. Moreover methods to read and write the data are also mentioned.Thus in this tutorial we have discussed in detail the concept of file handling learnbay data science, ibm data science certification, data science certification, data science courses in Bangalore, data science course fees, data science online courses, Trending data science course, IBM Data Science Professional Certificate, Deep Learning Specialization, – PowerPoint PPT presentation

Number of Views:3745
Slides: 17
Provided by: Learnbay.Datascience
Tags:

less

Transcript and Presenter's Notes

Title: File handling in Python


1
(No Transcript)
2
Python handling in Python
  • Python also supports file handling and allows use
    rs to handle files, i.e. read and write files, al
    ong with many other file handling options. 
  • Python handles files differently as text or binary
    , and this is important. 
  • Each line of code contains a sequence of characte
    rs and forms a text file. Each line of a file is t
    erminated by a special character called the EOL o
    r the End of the Line characters, such as the , 
    or newline characters. It ends the current line, a
    nd informs the interpreter that a new line has be
    gun.

3
  • Hence, in Python, a file operation takes place in
    the following order 1.Creating a file 2.Read
    or write into a file 3.Updating a
    file 4.Deleting a file
  • File handling in python is confined to text file
    and binary file.

4
(No Transcript)
5
Text file
  • Text fileModes of text file
  • Read mode - r
  •  Write Mode - w
  •  Append mode - a
  • read and write mode - r
  • write and read mode - w
  •  append and read mode - a

6
Opening and closing of a file
  • Syntax for opening a filefile object
    open(file_name , access_mode,
    buffering)file_name - The file_name argument
    is a string value that contains the name of the
    file that you want to access.
  • access_mode - The access_mode determines the mode
    in which the file has to be opened, i.e., read,
    write, append, etc. A complete list of possible
    values is given below in the table. This is
    optional parameter and the default file access
    mode is read (r).
  • buffering - If the buffering value is set to 0,
    no buffering takes place. If the buffering value
    is 1, line buffering is performed while accessing
    a file. If you specify the buffering value as an
    integer greater than 1, then buffering action is
    performed with the indicated buffer size. If
    negative, the buffer size is the system
    default(default behavior).
  • Syntax for Closing a filefileObject.close()

7
  •  Read mode - r If you open a file in read mode a
    nd if file exist then it will open the file and pl
    aces the cursor at the begining.If you open a fil
    e in read mode and if file does not exist then it 
    will give you the FileNotFoundError. Only reading
     is allowed, writing into the file is unsupported
     operation.
  •  Read and Write mode - rIf you open a file in r
    ead mode and if file exist then it will open the 
    file and places the cursor at the begining.
  •  If you open a file in read mode and if file d
    oes not exist then it will give you the FileNotFo
    undError.Reading and writing both are allowed.

8
  • Write Mode - wIf you open a file in write mode a
    nd if file exist then it will  open the
    file and places the cursor at the beginning
     of the file after deleting the content of the fi
    le.If you open a file in write mode and if file d
    oes not exist then it will create a file with the
     same name and places the cursor at the beginning
     of the file .Only writing is allowed
  •  
  •  write and read mode - wIf you open a file in w
    rite mode and if file exist then it will open the
     file and places the cursor at the beginning of th
    e file after deleting the content of the file.If
     you open a file in write mode and if file does no
    t exist then it will create a file with the same 
    name and places the cursor at the beginning of the
     file .Only writing and reading both are allowed

9
  •  append - a If you open a file in append mode an
    d if file exist then it will open the file and pl
    aces the cursor at the end of the file.If you open
     a file in append mode and if file does not exist 
    then it will create a file with the same name and
     places the cursor at the beginning of the file.
    Only writing is allowed, reading file the file is 
    unsupported operation, But it will always write t
    he content at the end of the file.
  • append and read mode - aIf you open a file in a
    ppend mode and if file exist then it will open the
     file and places the cursor at the end of the file
    .If you open a file in append mode and if file do
    es not exist then it will create a file with the 
    same name and places the cursor at the beginning 
    of the file.Only writing and reading both are all
    owed.

10
Operations on a binary file
  • Video files , audio files ,pdfs , images are all
    binary files.
  • All the operations work in the similar way as
  • Modes in binary file read - rb write - wb 
    append - ab read and write - rb
    write and read - wb append and read - ab

11
Methods to read the data
  •  read() - reads the whole content of the file and 
    returns  an string object.
  •  read(num of char) - reads the specified no of cha
    racters of the file  and returns an string object
    .
  •  readline() - reads one line at a tie and returns 
    a string object.
  • readlines() -reads all the line form
    the file and returns list of strings where every 
    trying will be one line

12
Example
  • f  open('abc.txt', 'r')out_data  f.readlines()
    print(out_data)print()print(type(out_data))f.cl
    ose()
  • O/P'What is Lorem Ipsum?\n', 'Lorem Ipsum is
    simply dummy text of the printing and typesetting
    industry. \n', "\\Lorem Ipsum has been the
    industry's standard dummy text ever since the
    1500s, when an unknown printer took a galley of
    type and scrambled it to make a type specimen
    book. \n", 'It has survived not only five
    centuries, but also the leap into electronic
    typesetting, remaining essentially unchanged.
    \n', 'It was popularised in the 1960s with the
    release of Letraset sheets containing Lorem Ipsum
    passages, and more recently with desktop
    publishing software like Aldus PageMaker
    including versions of Lorem Ipsum.\n', '\n', 'Why
    do we use it?\n', 'It is a long established fact
    that a reader will be distracted by the readable
    content of a page when looking at its layout.
    \n', "The point of using Lorem Ipsum is that it
    has a more-or-less normal distribution of
    letters, as opposed to using 'Content here,
    content here', making it look like readable
    English. Many desktop publishing packages and web
    page editors now use Lorem Ipsum as their default
    model text, and a search for 'lorem ipsum' will
    uncover many web sites still in their infancy.
    \n", 'Various versions have evolved over the
    years, sometimes by accident, sometimes on
    purpose (injected humour and the like).\n', 'If
    you open a file in write mode and if file exist
    then it will open the file and places the cursor
    at the begining fo the file after deleting the
    content of the file.' ltclass 'list'gt

13
Methods to write the data
  • write('str') - takes a string as input and writes 
    it into a file
  •  writelines(list of string)

14
Seek function
  • In Python, seek() function is used to change the
    position of the File Handle to a given specific
    position. File handle is like a cursor, which
    defines from where the data has to be read or
    written in the file.
  • Syntax f.seek(offset, from_what), where f is
    file pointer
  • ParametersOffset Number of postions to move
    forwardfrom_what It defines point of
    reference.The reference point is selected by the
     from_what argument. It accepts three values
  • 0 sets the reference point at the beginning of
    the file
  • 1 sets the reference point at the current file
    position
  • 2 sets the reference point at the end of the
    file

15
Tell method
  • The tell() method returns the current file
    position in a file stream.
  • Syntaxfile.tell()
  • Examplef open("demofile.txt",
    "r")print(f.readline())print(f.tell())
  • O/PHello! Welcome to demofile.txt32

16
Thanks for Watching!!!
  • For more follow us on our social media platforms
  • Instagram learnbay_datascience
  • Facebook learnbay
  • LinkedIn Learnbay
  • Twitter Learnbay1
Write a Comment
User Comments (0)
About PowerShow.com