Python File Handling

In Python, we can easily create, read, and operate on files. Python provides methods for creating and reading a new file, reading only specific parts of a file, writing data to an already created file, updating, deleting a file, etc.

Create a file in Python

The open() method is used in Python to create a new file. Let’s see a quick example:

A parameter is used in this method to work around the existence of an already created file with the same name i.e.

Create

The following will create a file, but an error will be shown if the file already exists:

Append

The following will create a file if it does not already exist:

Write

The following will create a file it does not already exist:

Read a file in Python

To read the content of a file in Python, use the read() method. At first, open the file using the open() method and then read it. Let us see an example:

Let’s say we have a file “demo.txt”, with the following content:

The file “demo.txt” is at the following location on our system:

Let us now read the contents of the file:

Output

Read some content (characters) of a Python file

We use the read() method to read the content of a file. To read only some (not all) content, use the same read() method. Specify the number of characters you want to read as a parameter.

Let us see an example to read only 10 characters from a file. We have the same “demo.txt” file as shown above:

Output

Read specific lines from a Python file

To read specific lines, use the readline() method. To read only one line, use the method once, for two lines, use it twice and this goes on.

Let us see an example to read only the 1st two lines from a file. We have the same “demo.txt” file as shown above:

Output

Write contents to a File

To write contents to an already created file, use the open() method in Python. For an existing file, either overwrite the new content or append content to the end of the file:

  • For overwrite, use the “w” parameter in the open() method
  • For append, use the “a” parameter in the open() method

Example – Overwrite content to an existing Python file

Let’s say we have a file “demo.txt”, with the following content:

Now, we will overwrite content to an existing file “demo.txt”.

Now, read the contents of the above file we updated:

Output

Example – Append content to an existing Python file

Let’s say we have a file “demo2.txt”, with the following content:

Now, we will append content to an existing file “demo2.txt”.

Now, read the contents of the above file we updated:

Output

Delete a File in Python

To delete a file in Python, we need to import a module os:

Let’s say, we have a file demo2.txt and we need to remove it. Use the os.remove() method:

In this tutorial, we learned how to handle files in Python. How to create, read, and write to a file in Python. We also saw how to delete a file.


Read More

How to install flask in Python on Windows 10
Python Lambda Functions
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading