Opening and closing files
Use the actual data files for reading and writing for standard input and output.
Python provides the basic functions and methods required to work with files by default. You can do most of the file manipulation using a file object.
Open function
Before you can read or write a file, you need to open it using the Python built-in open () function. This function creates a file object that will be used to call other support methods associated with it.
Syntax file object = open (file_name [, access_mode] [, buffering])
Here are the options:
file_name: The file_name argument is a string value containing the name of the file you want to access.
access_mode:. access_mode defines the mode in which the file should be opened, i.e. read, write, add, etc. A complete list of possible values ββis given in the table below. This is an optional parameter, and the default access mode is read (r).
buffering: If the buffering value is set to 0, buffering is not performed. If the buffering value is 1, when accessing the file, line buffering is performed. If you specify a buffering value as an integer greater than 1, then the buffering action is performed with the specified buffer size. If negative, the buffer size is the system default (default behavior).
Here is a list of different ways to open a file -
Modes and Description r = Opens a read-only file. The file pointer is placed at the beginning of the file. This is the default mode.
rb = Opens a file for reading in binary format only. The file pointer is placed at the beginning of the file. This is the default mode.
r + = Opens a file for reading and writing. The file pointer is placed at the beginning of the file.
rb + = Opens a file for reading and writing in binary format. The file pointer is placed at the beginning of the file.
w = Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, a new file is created for writing.
wb = Opens a file for writing in binary format only. Overwrites the file if the file exists. If the file does not exist, a new file is created for writing.
w + = Opens a file for writing and reading. Overwrites an existing file if the file exists. If the file does not exist, a new file is created for reading and writing.
wb + = Opens a file for writing and reading in binary format. Overwrites an existing file if the file exists. If the file does not exist, a new file is created for reading and writing.
a = Opens a file to add. The file pointer is at the end of the file if the file exists. That is, the file is in upload mode. If the file does not exist, it creates a new file for writing.
ab = Opens a file to add in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in upload mode. If the file does not exist, it creates a new file for writing.
a + = Opens a file for adding and reading. The file pointer is at the end of the file if the file exists. The file opens in upload mode. If the file does not exist, it creates a new file for reading and writing.
ab + = Opens a file for adding and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in upload mode. If the file does not exist, it creates a new file for reading and writing.
Reading and writing files
A file object provides a set of access methods that make our life easier using the read () and write () methods to read and write files.
Write () method
The write () method writes any line to an open file. It is important to note that Python strings can have binary data, not just text.
The write () method does not add a newline character ('\ n') to the end of the line -
Syntax
fileObject.write(string);
Here the passed parameter is the content that should be written to the open file.
Example
# Open a file fo = open("file.txt", "wb") fo.write( "Python is a great language");
The above method will create a foo.txt file and write the given content in this file, and finally it will close this file. If you open this file, it will have the following content.
Python is a great language.
Read () method
The read () method reads a line from an open file. It is important to note that Python strings can have binary data. except text data.
Syntax
fileObject.read([count]);
Here, the parameter passed is the number of bytes that should be read from the open file. This method starts reading from the beginning of the file, and if there is no account, then it tries to read as much as possible, possibly to the end of the file.
Example
Take the foo.txt file we created above.
# Open a file fo = open("foo.txt", "r+") str = fo.read(10); print "Read String is : ", str
This gives the following result: Reading a line: Python