Skip to main content

Command Palette

Search for a command to run...

Manipulate files in python.

Published
4 min readView as Markdown
Manipulate files in python.
M

Software Engineer | Full Stack Web developer | Javascript | React.js | Python | PHP | AWS | Docker

Manipulate files in python.

We will be able to perform all kinds of manipulations on our files in Python: creating a file, reading data, writing, etc. To perform these operations, however, you will first have to open the file. For this, we use the > fopen()function.

The > fopen() function returns a “file” type object. This function allows us to open a file to perform different operations.

We will pass two arguments to the > fopen() function: the name of the file to open and the opening mode (which is r by default). This opening mode will condition the operations that will be able to be done on the file afterward. The most used opening modes that will interest us are the following:

Opening mode =Description

r= Open a read-only file. The file cannot be modified. The internal pointer is placed at the start of the file.

r+= Opens a file for reading and writing. The internal pointer is placed at the start of the file.

a Opens a write-only file preserving existing data. The internal pointer is placed at the end of the file, so new

data will be appended at the end. If the file does not exist, create it.

a+ Opens a file for reading and writing keeping the existing data. The internal pointer is placed at the end of the file, so new data will be appended at the end. If the file does not exist, create it.

w Opens a write-only file. If the file exists, the existing information will be deleted. If it does not exist, create a file.

w+ Opens a file for reading and writing. If the file exists, the existing information will be deleted. If it does not exist, create a file. Note that we will also be able to add another letter behind the mode to define whether the file should be opened in text mode (letter t, default value or in binary mode (letter b). In this course, we will focus on the text mode and will not discuss binary mode, which is generally less used.

Close a file

Once you have finished working with a file, it is considered good practice to smoke it. This avoids using resources unnecessarily and obtaining certain unexpected behaviors.

To close a file, you can either use the close() method or ideally add the keyword before open() when opening the file, which will guarantee that the file will be closed automatically once the operations have been completed.

The position of the internal cursor or pointer Before going further in file manipulation, I must tell you about the position of the cursor (or "file pointer") because it will impact the result of most of the manipulations that we will be able to perform. on the files. It is therefore essential to always know where this pointer is located and also to know how to move it.

The place in a file from which an operation will be made is the cursor or pointer. To give a concrete example, the cursor in a Word document, in a form field, or when you perform a Google search or type a URL in your browser is the blinking bar.

This cursor indicates the location from which you will write your query or delete a character, etc. The cursor in the files will be exactly the same except that here we cannot see it visually.

The opening mode chosen will be the first thing that will influence the position of the pointer. Indeed, depending on the chosen mode, the file pointer will be located in a different place. Then, you should know that some methods will move this cursor during their execution, such as the methods for reading the file.

For example;

To know the place of the internal pointer in a file and to move this pointer, we will be able to use the > tell() and seek() methods.

The > tell()method returns the position of the internal pointer. The seek() method allows you to reposition this pointer.

The > fseek() method takes two arguments: the first argument indicates how much or wants to shift the internal cursor while the second argument specifies the reference point from which to shift the pointer. This reference point can either be 0 for the beginning of the file, 1 for the current position, or 2 for the end of the file.

Among other things, seek() will only accept as offset value 0 or the value returned by > tell() and the reference point can only be the beginning or the end of the file.

More from this blog

Mbiakop Clinton's blog

30 posts