1.1 Files in python
v
File: -
- File is a collection of data or information which is saved permanently in the non-volatile memory (Hard disk).
- Python supports file handling and allows the user to handle the files by read, write and other operations.
- Python supports two types of files:
1. Text File.
2. Binary File.
1) Text File: -
- Text file stores data in the form of text and readable by human and computer.
- Text files data is arranged in a structured as a sequence of lines.
- Each line is terminated with a special character called end of the line character (EOL).
- The most common special character comma (,) is used to represent new line character.
2) Binary File: -
- Binary file stores the data in the form of Binary and readable by computer only.
- Binary files can only be processed by an application that can understand the file structure.
- It can be performing the operations Like read and interpret binary.
· In python a
file operation takes place in the following order.
ü Open a file.
ü Perform read
or write operation.
ü Close the
file.
vCreating
Text file: -
- We can create a .txt files by using python code.
- To create a text file, we use following code
- Python has a built-in function open( ) to open a file.
- Once the file is opened, we can perform read, write, append, etc. operations on that file.
Parameters:
1. file_name: The
file_name is a string parameter that contain name of the file. It is the name
of the file which we want to access.
2. access_mode:
Define
the mode in which file will be open i.e., read, write, append, etc. it is
optional parameter. Default mode is read.
3. buffer_size:
The
optional buffering argument specifies the file’s desired buffer size.
·
0 means no buffer.
·
1 means linear buffer.
·
(-)ve buffer means system’s default buffer size.
·
(+)ve buffer means value greater than 1, define
approximate size of buffer.
4. encoding: Specify
encoding algorithm.
vFile access
modes:
- Python provides different modes to open a file.
- We can specify the read, write or append mode while opening a file.
- We can also specify, if we want to open the file in text mode or binary mode.
- The default is reading the text mode. In this mode, we get strings when reading from file.
- Binary mode returns bytes and this mode is used when dealing with non-text files like images or .exe files.
- List of different modes of opening a file.
|
Mode |
Description |
|
r |
Open a file for reading only. This is
default mode. |
|
w |
Open a file for writing only. If file
already exist then it will overwrite the file otherwise it creates a file and
opens the file. |
|
rb |
Open a file for reading only in binary
format. |
|
wb |
Open a file for writing only in binary
format. If file already exist then it will overwrite the file otherwise it
creates a file and opens the file. |
|
r+ |
Open file for both reading and writing. |
|
w+ |
Open file for both reading and writing. If
file already exist then it will overwrite the file otherwise it creates a
file and opens the file. |
|
rb+ |
Open file for both reading and writing in
binary format. |
|
wb+ |
Open file for both reading and writing in
binary format. If file already exist then it will overwrite the file
otherwise it creates a file and opens the file. |
|
a |
Open file for appending. If file already
exist then pointer will set at the end of the file otherwise it creates a new
file. |
|
ab |
Open file for appending in binary format.
If file already exist then pointer will set at the end of the file otherwise
it creates a new file. |
|
a+ |
Open file for appending and reading. If
file already exist then pointer will set at the end of the file otherwise it
creates a new file. |
|
ab+ |
Open file for appending and reading in
binary format. If file already exist then pointer will set at the end of the
file otherwise it creates a new file. |
File
object attributes: list of all attributes related to file object –
1.
file.closed : Returns true if file is closed,
otherwise it returns false.
2.
file.mode : Returns access mode with which file was
opened.
3.
file.name : Returns name of the file.
To close the file, we use close( ) method. It closes the file object, after no more writing can be done.
Example:
-
Output:
v
File methods to write data: -
- Python provides different ways to write the data into file.
- In this first, we need to create the file using built-in python method “open( )” with “w” - (write) mode and stores it in file-object.
- The “w” option will delete any previous existing file and create a new file to write.
|
Mode |
Meaning |
|
"w" |
Write (overwrite if file
exists) |
|
"a" |
Append (write at the end of
file) |
|
"w+" |
Write and read |
|
"a+" |
Append and read |
Ø There are
two methods to insert the data in files.
1. write( ).
2. writelines(
).
1) write( ): -
- The first method is write( ), which writes or insert a string to the file without adding a newline automatically.
- It does not add newline characters automatically, so we must include \n in each string if we want separate lines.
- If the file does not exist, it will be created, and if it already exists, it will be overwritten.
- It is useful for writing small amounts of text.
- For example,
f.write("Hello, Python!")
writes the
text directly into the file.
Example: -
output: -
2) writelines(
): -
- The writelines ( ) method is used to write or insert a list of strings to the file.
- It does not add newline characters automatically, so we must include \n in each string if we want separate lines.
- Python’ writelines( ) method takes a list of lines as input and writes to a file object that is open with write / append that can ins all the lions at the same time to a fight Python writelines method takes a list of lines as input and writes to a file object that is open with provide or append access.
Example1: -
output: -
output: -
v File methods to read data: -
- Python provide different ways to read the data from a file.
- The read( ) method reads a string from an open file.
- We can read whole data or line by line data from a file.
- We have three different methods to read the data from the file.
- read( ) method.
- readline( ) method.
- readlines( ) method.
1) read( )
method: -
- The read( ) method is used to read all data of a file at once.
- By default, the read( ) method returns the whole text, but we can also specify how many characters we want to return.
- However, if the file is very large, reading it all at once may consume a lot of memory.
Example1: -
output: -
- We can also use the read(size) method to read in size amount of data. If the size parameter is not specified it reads and returns up to the end of the file.
Example2: -
output: -
2) readline( )
method: -
- The readline( ) method reads one line from the file at a time.
- Each time it is called, it moves the file pointer to the next line.
- This method is useful when we want to process a file line by line without loading the entire file into memory.
- This method reads a file till the new line, including the new line character.
Example: -
Output: -
3) readlines( )
method: -
- The readlines( ) method reads all lines from the file and returns them as a list of strings.
- Each string in the list contains a line from the file, including the newline character \n at the end.
- This method is convenient when we need to work with all lines at once.
Example:
-
Output:
-