11.1.2 File PositionOne of the attributes of an open file is its file position that keeps track of where in the file the next character is to be read or written. On GNU systems, and all POSIX.1 systems, the file position is simply an integer representing the number of bytes from the beginning of the file. Show
The file position is normally set to the beginning of the file when it is opened, and each time a character is read or written, the file position is incremented. In other words, access to the file is normally sequential. Ordinary files permit read or write operations at any position within the file. Some other kinds of files may also permit this. Files which do permit this are sometimes referred to as random-access files. You can change the file position using the Streams and descriptors that are opened for append access are treated specially for output: output to such files is always appended sequentially to the end of the file, regardless of the file position. However, the file position is still used to control where in the file reading is done. If you think about it, you’ll realize that several programs can read a given file at the same time. In order for each program to be able to read the file at its own pace, each program must have its own file pointer, which is not affected by anything the other programs do. In fact, each opening of a file creates a separate file position. Thus, if you open a file twice even in the same program, you get two streams or descriptors with independent file positions. By contrast, if you open a descriptor and then duplicate it to get another descriptor, these two descriptors share the same file position: changing the file position of one descriptor will affect the other. Files are named locations on disk to store related information. They are used to permanently store data in a non-volatile memory (e.g. hard disk). Since Random Access Memory (RAM) is volatile (which loses its data when the computer is turned off), we use files for future use of the data by permanently storing them. When we want to read from or write to a file, we need to open it first. When we are done, it needs to be closed so that the resources that are tied with the file are freed. Hence, in Python, a file operation takes place in the following order:
Opening Files in PythonPython has a built-in
We can specify the mode while opening a file. In mode, we specify whether we want to
read The default is reading in text mode. In this mode, we get strings when reading from the file. On the other hand, binary mode returns bytes and this is the mode to be used when dealing with non-text files like images or executable files.
Unlike other languages, the character Moreover, the default encoding is platform dependent. In windows, it is So, we must not also rely on the default encoding or else our code will behave differently in different platforms. Hence, when working with files in text mode, it is highly recommended to specify the encoding type.
Closing Files in PythonWhen we are done with performing operations on the file, we need to properly close the file. Closing a file will free up the resources that were tied with the file. It is done using the Python has a garbage collector to clean up unreferenced objects but we must not rely on it to close the file.
This method is not entirely safe. If an exception occurs when we are performing some operation with the file, the code exits without closing the file. A safer way is to use a try...finally block.
This way, we are guaranteeing that the file is properly closed even if an exception is raised that causes program flow to stop. The best way to close a file is by using the We don't need to explicitly call the
Writing to Files in PythonIn order to write into a file in Python, we need to open it in write We need to be careful with the Writing a
string or sequence of bytes (for binary files) is done using the
This program will create a new file named We must include the newline characters ourselves to distinguish the different lines. Reading Files in PythonTo read a file in Python, we must open the file in reading
There are various methods available for this purpose. We can use the We can read the
We can see that the We can change our current file cursor (position) using the
We can read a file line-by-line using a for loop. This is both efficient and fast.
In this program, the lines in the file itself include a newline character Alternatively, we can use the
Lastly, the
Python File MethodsThere are various methods available with the file object. Some of them have been used in the above examples. Here is the complete list of methods in text mode with a brief description:
What is a files read position?A file's read position marks the location of the next item that will be read from the file. Initially, the read position is set to the beginning of the file.
Where is the read position when an input file is opened?When an input file is opened, its read position is initially set to the first item in the file. When a file that already exists is opened in append mode, the file's existing contents are erased. If you do not handle an exception, it is ignored by the Python interpreter, and the program continues to execute.
When working with this type of file you access its data from the beginning of the file to the end of the file?When working with a sequential access file, you access its data from the beginning of the file to the end of the file.
What is the purpose of opening a file Opening a file creates a connection between the file and the program?Opening a file creates a connection between the file and the program. It also creates an association between the file and a file object.
|