File manipulation in Python is an essential skill for any Python developer. Python offers several functions and methods for manipulating files. In this chapter, we'll discuss how Python can be used to manipulate files.

Opening Files

To open a file in Python, we use the open() function. This function returns a file object that contains methods and attributes that can be used to gather information about the file and manipulate it. The syntax for opening a file in Python is:

file_object = open("filename", "mode")

Where filename is the name of the file you want to open and mode is the mode in which you want to open the file. The mode can be 'r' for reading, 'w' for writing, 'a' for adding, 'r+' for reading and writing, and 'b' for opening the file in binary mode.

Reading Files

Once a file is opened, you can read the contents of the file using the read() method. This method reads the entire contents of the file as a string. If size is specified, it reads the specified number of bytes from the file. Here is an example:

file_object = open("filename", "r")
print(file_object.read())

Writing to Files

Python allows writing to a file using the write() method. This method writes the given string to the file. If the file was opened in text mode ('t'), it must be a string. If the file was opened in binary mode ('b'), it must be a byte object. Here is an example:

file_object = open("filename", "w")
file_object.write("Hello, world!")

Close Files

Once you've finished working with a file, it's important to close it using the close() method. This frees up resources used to work with the file. Here is an example:

file_object = open("filename", "r")
print(file_object.read())
file_object.close()

File Manipulation with the os Module

Python provides the os module which contains several useful functions for manipulating files and directories. For example, the os.rename() function can be used to rename a file. Here is an example:

import os
os.rename("old_filename", "new_filename")

The os module also provides the os.remove() function for deleting a file. Here is an example:

import os
os.remove("filename")

Directory Manipulation with the os Module

The os module also provides functions for manipulating directories. For example, the os.mkdir() function can be used to create a new directory. Here is an example:

import os
os.mkdir("directory_name")

Similarly, the os.rmdir() function can be used to remove a directory. Here is an example:

import os
os.rmdir("directory_name")

In short, file manipulation in Python is a crucial skill every Python developer should possess. Python provides a variety of functions and methods for manipulating files, making the task of manipulating files a breeze.

Now answer the exercise about the content:

What is the function used to open a file in Python and what are the possible ways to open a file?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Exception Handling in Python

Next page of the Free Ebook:

48Exception Handling in Python

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text