KEMBAR78
File Operations in python Read ,Write,binary file etc. | PPTX
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.

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

  • 1.
  • 2.
    Introduction • A widerange 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 ofa 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 ofa 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 ofa 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 • Ifyou 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 • Tocreate 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 • Youcan 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 fileif 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 intoa 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 froma file • Read a file using read() • You can also use readline() and readlines().
  • 14.
    Close file • Weneed 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 valuesfor 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 canget 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 anddirectories in a python We can copy single and multiple files using different methods and the most commonly used one is the shutil.copy() method.