SlideShare a Scribd company logo
3
Most read
4
Most read
7
Most read
File Operations
Introduction
• A wide range of functions in python is capable to cater
the need of file operations such as
opening,reading,writing,creating,deleting a file,closing
and various file methods.
• To store a data temporarily and permanently,we use
files.
• A file is a collection of data stored on a disk in one unit
identified by filename.
Creating a file
• We can create a file in the current directory or a specified
directory.
• We don’t have to import any module to create a new file.
• We can create a file using the built-in function open().
• Syntax :
open(‘file_path’,’access_mode’)
Access Mode of a file
File
Mode
Meaning
w Create a new file for writing.If a file already
exits,it truncates the file first.
r Open a file for reading
a Open a file in the append mode and add new
content at the end of the file.
b Create a binary a file
x Open a file only for exclusive creation.If the file
already exists this operation fails
Access Mode of a file
File
Mode
Meaning
wb It opens the file to write only in binary format.
w+ Open a file to write and read data.
wb+ Opens the file to write and read both in binary
format.
a+ It opens a file to append and read both.
Access Mode of a file
File Mode Meaning
rb It opens the file to read only in binary format.The
file pointer exists at the beginning.
r+ It opens the file to read and write both.The file
pointer exists at the beginning.
rb+ It opens the file to read and write both in binary
format.The file pointer exists at the beginning.
Important Note
• If you have not specified any specific path(directory
location),the file is created in the working directory.It
is known as creating a file using the relative path.A
relative path contains the current directory and then
the file name.
Important Note
• To create a file inside a specified directory,we need to
open a file using the absolute path.An absolute path
contains the entire path to the file or directory that we
need to use.
join() method
• You can join directory path and file name to create a file
at the specified location.
• If you have a directory path and file name in two
variables use the os.path.join() function to construct a full
path.
• This function accepts the directory path and file name as
arguments and constructs an absolute path to create a
file.
Create a file if not exists
We can create a file only if it is not present using the
following two ways:
Use os.path.exists("file path") function to check if a file
exists.
 Use the access mode x in the open() function and
exception handling.
Open a file
• The data can be in the form of files such as text, csv, and
binary files. To extract data from these files, Python comes
with built-in functions to open a file and then read and
write the file’s contents.
• The access mode specifies the operation you wanted to
perform on the file,such as reading or writing.
• To open and read a file,use the r access mode
• To open a file for writing ,use the w mode.
Write contents into a file
• If you have opened a file in a write mode ,you can
write or append text to the file using the write()
method.
• You can also use writeline() and writelines().
Read contents from a file
• Read a file using read()
• You can also use readline() and readlines().
Close file
• We need to make sure that the file will be closed
properly after completion the file operation.
• Use close() to close a file.
seek() and tell()
• The seek () function sets the position of a file pointer and the tell()
function returns the current position of a file pointer.
• A file handle or pointer denotes the position from which the file
contents will be read or written.File handle is also called as file
pointer or cursor.
• When you read file line by line,the file pointer moves one line at a
time.
• To read a specific portion of the file in such cases use the seek()
method to move the file pointer to that position.
• Syntax:
f.seek(offset,whence) where whence is a reference point
Continue….
The allowed values for the whence argument are: –
•A whence value of 0 means from the beginning of the file.
•A whence value of 1 uses the current file position
•A whence value of 2 uses the end of the file as the
reference point.
Continue…
• We can get the file handle current position using the
tell() method.
• Syntax:
• File_object.tell()
• To use the seek() and tell() methods to manipulate the
file handle position to add new content or read certain
portion of the file.
to use the seek() and tell() methods to manipulate the filehandle position to add new content or read certain portions of the file.
Delete a file
• In python the os module provides remove () function to remove or
delete file path.
Check file size
• os.path module:
os.path.getsize(‘file_path’): Returns the file size in bytes.
os.stat(file).st_size: Returns the file size in bytes.
• pathlib.Path(‘path’).stat().st_size: Returns the file size in bytes.
Copy files and directories in a
python
We can copy single and multiple files using
different methods and the most commonly used
one is the shutil.copy() method.
File Operations in python Read ,Write,binary file etc.

More Related Content

PPTX
file handling in python using exception statement
PPT
Python file handlings
PPTX
Unit V.pptx
PPT
File Handling as 08032021 (1).ppt
PDF
Python Files I_O17.pdf
PPTX
File Handling Python
PPTX
pspp-rsk.pptx
PDF
23CS101T PSPP python program - UNIT 5.pdf
file handling in python using exception statement
Python file handlings
Unit V.pptx
File Handling as 08032021 (1).ppt
Python Files I_O17.pdf
File Handling Python
pspp-rsk.pptx
23CS101T PSPP python program - UNIT 5.pdf

Similar to File Operations in python Read ,Write,binary file etc. (20)

PPTX
File handling in Python
DOCX
File Handling in python.docx
PDF
Python - Lecture 8
PPTX
Data File Handling in Python Programming
PPTX
DFH PDF-converted.pptx
PDF
File and directories in python
PPTX
What is FIle and explanation of text files.pptx
PPTX
01 file handling for class use class pptx
PPT
Python File functions
PPTX
FILE HANDLING.pptx
PPTX
Chapter 08 data file handling
PDF
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
PPTX
FILE INPUT OUTPUT.pptx
PPTX
Python UNIT-III-part-1.pptx File Handling
PPTX
this is about file concepts in class 12 in python , text file, binary file, c...
PPTX
Chapter - 5.pptx
PPTX
file_handling_python_bca_computer_python
PPTX
Module 5_Reading and Writing Files.pptx.
PPTX
File handling for reference class 12.pptx
PPTX
5-filehandling-2004054567151830 (1).pptx
File handling in Python
File Handling in python.docx
Python - Lecture 8
Data File Handling in Python Programming
DFH PDF-converted.pptx
File and directories in python
What is FIle and explanation of text files.pptx
01 file handling for class use class pptx
Python File functions
FILE HANDLING.pptx
Chapter 08 data file handling
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
FILE INPUT OUTPUT.pptx
Python UNIT-III-part-1.pptx File Handling
this is about file concepts in class 12 in python , text file, binary file, c...
Chapter - 5.pptx
file_handling_python_bca_computer_python
Module 5_Reading and Writing Files.pptx.
File handling for reference class 12.pptx
5-filehandling-2004054567151830 (1).pptx
Ad

More from deepalishinkar1 (20)

PPTX
data structure stack appplication in python
PPTX
Stack application in infix to prefix expression
PPTX
Data Structure Stack operation in python
PPTX
steps for template in django for project
PDF
Web application on menu card qrcode generator.pdf
PPTX
Django model create a table in django web framework
PPTX
TO DO APP USING STREAMLIT PYTHON PROJECT
PPTX
basic concepts of object oriented in python
PDF
Inheritance and polymorphism oops concepts in python
PPTX
DATABASE CONNECTIVITY PYTHON USING MYSQL/SQLITE/POSTGRE
PDF
How to create a django project procedure
PDF
Virtual environment in python on windows / linux os
PPTX
Operators in python
PPTX
Data handling in python
PDF
Practical approach on numbers system and math module
PDF
Demonstration on keyword
PPTX
Numbers and math module
PPTX
Basic concepts of python
PPTX
Introduction to python
PDF
Set methods in python
data structure stack appplication in python
Stack application in infix to prefix expression
Data Structure Stack operation in python
steps for template in django for project
Web application on menu card qrcode generator.pdf
Django model create a table in django web framework
TO DO APP USING STREAMLIT PYTHON PROJECT
basic concepts of object oriented in python
Inheritance and polymorphism oops concepts in python
DATABASE CONNECTIVITY PYTHON USING MYSQL/SQLITE/POSTGRE
How to create a django project procedure
Virtual environment in python on windows / linux os
Operators in python
Data handling in python
Practical approach on numbers system and math module
Demonstration on keyword
Numbers and math module
Basic concepts of python
Introduction to python
Set methods in python
Ad

Recently uploaded (20)

PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
From loneliness to social connection charting
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharma ospi slides which help in ospi learning
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
Pre independence Education in Inndia.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Renaissance Architecture: A Journey from Faith to Humanism
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Cell Structure & Organelles in detailed.
Open Quiz Monsoon Mind Game Final Set.pptx
O7-L3 Supply Chain Operations - ICLT Program
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
From loneliness to social connection charting
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharma ospi slides which help in ospi learning
The Final Stretch: How to Release a Game and Not Die in the Process.
Pre independence Education in Inndia.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Pharmacology of Heart Failure /Pharmacotherapy of CHF
NOI Hackathon - Summer Edition - GreenThumber.pptx

File Operations in python Read ,Write,binary file etc.

  • 2. Introduction • A wide range of functions in python is capable to cater the need of file operations such as opening,reading,writing,creating,deleting a file,closing and various file methods. • To store a data temporarily and permanently,we use files. • A file is a collection of data stored on a disk in one unit identified by filename.
  • 3. Creating a file • We can create a file in the current directory or a specified directory. • We don’t have to import any module to create a new file. • We can create a file using the built-in function open(). • Syntax : open(‘file_path’,’access_mode’)
  • 4. Access Mode of a file File Mode Meaning w Create a new file for writing.If a file already exits,it truncates the file first. r Open a file for reading a Open a file in the append mode and add new content at the end of the file. b Create a binary a file x Open a file only for exclusive creation.If the file already exists this operation fails
  • 5. Access Mode of a file File Mode Meaning wb It opens the file to write only in binary format. w+ Open a file to write and read data. wb+ Opens the file to write and read both in binary format. a+ It opens a file to append and read both.
  • 6. Access Mode of a file File Mode Meaning rb It opens the file to read only in binary format.The file pointer exists at the beginning. r+ It opens the file to read and write both.The file pointer exists at the beginning. rb+ It opens the file to read and write both in binary format.The file pointer exists at the beginning.
  • 7. Important Note • If you have not specified any specific path(directory location),the file is created in the working directory.It is known as creating a file using the relative path.A relative path contains the current directory and then the file name.
  • 8. Important Note • To create a file inside a specified directory,we need to open a file using the absolute path.An absolute path contains the entire path to the file or directory that we need to use.
  • 9. join() method • You can join directory path and file name to create a file at the specified location. • If you have a directory path and file name in two variables use the os.path.join() function to construct a full path. • This function accepts the directory path and file name as arguments and constructs an absolute path to create a file.
  • 10. Create a file if not exists We can create a file only if it is not present using the following two ways: Use os.path.exists("file path") function to check if a file exists.  Use the access mode x in the open() function and exception handling.
  • 11. Open a file • The data can be in the form of files such as text, csv, and binary files. To extract data from these files, Python comes with built-in functions to open a file and then read and write the file’s contents. • The access mode specifies the operation you wanted to perform on the file,such as reading or writing. • To open and read a file,use the r access mode • To open a file for writing ,use the w mode.
  • 12. Write contents into a file • If you have opened a file in a write mode ,you can write or append text to the file using the write() method. • You can also use writeline() and writelines().
  • 13. Read contents from a file • Read a file using read() • You can also use readline() and readlines().
  • 14. Close file • We need to make sure that the file will be closed properly after completion the file operation. • Use close() to close a file.
  • 15. seek() and tell() • The seek () function sets the position of a file pointer and the tell() function returns the current position of a file pointer. • A file handle or pointer denotes the position from which the file contents will be read or written.File handle is also called as file pointer or cursor. • When you read file line by line,the file pointer moves one line at a time. • To read a specific portion of the file in such cases use the seek() method to move the file pointer to that position. • Syntax: f.seek(offset,whence) where whence is a reference point
  • 16. Continue…. The allowed values for the whence argument are: – •A whence value of 0 means from the beginning of the file. •A whence value of 1 uses the current file position •A whence value of 2 uses the end of the file as the reference point.
  • 17. Continue… • We can get the file handle current position using the tell() method. • Syntax: • File_object.tell() • To use the seek() and tell() methods to manipulate the file handle position to add new content or read certain portion of the file. to use the seek() and tell() methods to manipulate the filehandle position to add new content or read certain portions of the file.
  • 18. Delete a file • In python the os module provides remove () function to remove or delete file path.
  • 19. Check file size • os.path module: os.path.getsize(‘file_path’): Returns the file size in bytes. os.stat(file).st_size: Returns the file size in bytes. • pathlib.Path(‘path’).stat().st_size: Returns the file size in bytes.
  • 20. Copy files and directories in a python We can copy single and multiple files using different methods and the most commonly used one is the shutil.copy() method.