Introduction
| Name | Description |
| file_name | The file_name is a string parameter that contains the name of the file |
| access_mode | Define the mode in which the file will be open. It is an optional parameter. The default mode is read. |
| buffer_size | The optional buffering argument specifies the file’s desired buffer size.For file operation, python use the system’s default buffering unless we configure the buffer size. We can configure the buffer size in the following ways.
• 1 means linear buffer • (-)ve buffer means system’s default buffer size • (+)ve value greater than 1 defines the approximate size of the buffer |
| encoding | Specify encoding Algorithm |
Access Mode:
Python provides different modes to open a file. Some basic modes are the following:
| Mode | Description |
| r | Open a file for reading only. This is the default mode. |
| w | Open file for writing only. If the file exists then it will overwrite the file otherwise first create a file and after then open the file. |
| rb | Open file for reading only in binary format. |
| wb | Open file for writing only in binary format. If the file doesn’t exist it will first create a file and after then open the file |
| r+ | Open file for both reading and writing. |
| w+ | Open a file for both writing and reading. If the file exists then it will overwrite the file otherwise first create a file and after then open the file. |
| rb+ | Open file for both reading and writing in binary format. |
| wb+ | Open a file for both writing and reading in binary format. If the file exists then it will overwrite the file otherwise first create a file and after then open the file. |
| a | Open file for appending. If the file already exists then the pointer will set at the end of the file otherwise a new file will create. |
| ab | Open file for appending in binary format. If the file already exists then the pointer will set at the end of the file otherwise a new file will create. |
| a+ | Open file for appending and reading. If the file already exists then the pointer will set at the end of the file otherwise a new file will create. |
| ab+ | Open file for appending and reading in binary format. If the file already exists then the pointer will set at the end of the file otherwise a new file will create. |
Example:
- File_Obj=open("News.txt","w");
- print("File Name=",File_Obj.name);
- print("File Open Mode=",File_Obj.mode);
- print("File is Closed=",File_Obj.closed);
- print("Encoding Algo is =",File_Obj.encoding);
- File_Obj.close(); #Close The file
- print("File is Closed=",File_Obj.closed)
File Name= News.txt
File Open Mode= w
File is Closed= False
Encoding Algo is = cp1252
File is Closed= True
- #Open File
- File_Obj=open("News.txt","w");
- #Write Data
- File_Obj.write("My Name is Pankaj Kumar Choudhary \n");
- File_Obj.write("I Live in Alwar Rajasthan \n");
- File_Obj.write("I Love Programming \n");
- File_Obj.write("I am doing my B.Tech. from Alwar");
- #Close File
- File_Obj.close();

- #Open File
- File_Obj=open("News.txt","r");
- #Read Data
- Read_=File_Obj.read();
- print(Read_);
- #Close File
- File_Obj.close();








- 0 means beginning of the file
- 1 means reference of the current line
- 2 means the end of the file.
Example:
remove() Method
The remove() method is used to remove(delete) any existing file. To remove any file just pass the name of that file into remove() method.
Syntax:
os.remove(File_Name)
Example:
rename() Method
The rename() method is used to provide a new name to an existing file. The rename() method takes two parameters, first one is the name of the existing file and the second is the new name of the file.
Syntax:
os.rename(Old_Name,New_Name);
fileno() Method
The fileno() method is used by the underlying implementation to request I/O operations from the operating system. This can be useful for other, lower level interfaces that use file descriptors, such as the fcntl module or os.read().
Example:
truncate() Method
The truncate() method truncates the file’s size. It means truncate() method resizes the stream to the given size in bytes. The current stream position isn’t changed. If the specified size of the file exceeds the current size, then the result is platform-dependent. This method would not work in case the file is opened in read-only mode
Example:
In the above example, we truncate all the files after 30 bytes. So now this file only contains 30 bytes of data.
Example:
This example is the same as the previous example but in this example, we use “os.stat()” method to identify the size of the file.
writelines() Method
The writelines() method is used to write a list of lines to the stream. The list is used to writing multiple lines in the file.
Example:
Example:
readable() return true if read operation is possible otherwise return false.
writable() return true if write operation is possible otherwise return false.
seekable() return true if the stream supports random access.


Example:





- File_Obj=open("NEWS.txt","r");
- print("File is Readable=",File_Obj.readable());
- print("File is Writable=",File_Obj.writable());
- print("File is Seekable=",File_Obj.seekable());
File is Writable= False
File is Seekable= True

Sarathlal SaseendranPosted Jun 19, 2019, 10:07 AM
Very nice article