Skip to content

Python File Handling Introduction | Create, Open, Write & Delete

  • by

In this tutorial, you will learn about Python File Handling. Before going to code and definition let’s understand the need or what it is. Think about you have a Notepad where you can write, read, edit, and delete content (text or words or sentences), even Notepad can open text files. So now think you have an application that has to read data from a file (excel sheet, doc file, text file, cv, etc) or add. That time you needed an open a file with a program.

Python File Handling Introduction and examples how to do

Here we will learn and see examples of how to Create, Open, Read, Write, and Delete files in Python, which is called Python File Handling.

Python can do the same except you need like write files, grab the text of the text file and create a text file. In Python read and write and the process is straightforward. You just need to practice,

The Python program that does the file handling. These are built-in functions, so you don’t need third-party libraries.

OperationSyntaxDescription
Create a Filefile = open("filename", "w")Creates a new file with the given filename in write mode.
Open a Filefile = open("filename", "mode")Opens an existing file with the given filename in the specified mode.
Write to a Filefile.write("text")Writes the specified text to the file.
Close a Filefile.close()Closes the file, releasing system resources.
Delete a Fileimport os<br>os.remove("filename")Deletes the specified file.

Note: Replace “filename” with the actual name of the file you want to create, open, or delete. Replace “mode” with the desired file mode, such as "r" (read mode), "w" (write mode), or "a" (append mode).

Python File Handling

The Main function for working with files (folders) in Python is the open() function.

The open() the function takes two parameters first filename and another mode. The filename will be a file path with name and mode per the required operation on the file.

Modes in File Handling

  • w write mode – if the file doesn’t exist create it and open it in write mode. The stream is positioned at the beginning of the file.
  • r read mode – (Only read the file ) is a default in the open function. The stream is positioned at the beginning of the file.
  • a append mode (if the file doesn’t exist create it and open it in append mode). The stream is positioned at the end of the file.
  • w+ create a file – if it doesn’t exist and open it in write mode.
  • r+ open an existing file in read+write mode.
  • a+ – if it doesn’t exist and opens it in append mode.

Create File

We are using w mode to create the file. If the file doesn’t exist create it and open it.

f = open("cFile.txt", "w")

A complete tutorial about how to create a file read this tutorial: Python Create File (Empty Text File)

Write File

Assume we have the empty “testFile.txt” file, located in the same folder as Python. (if the file does not exist, then it will create)

Now writing the file. w – Write mode

f = open("testFile.txt", "w")
f.write("Hello file")

For a Detailed tutorial about Writing a File in Python read this post: Python Write to Text File

Read File & Open a File

Assume we have the “testFile.txt” file, located in the same folder as Python. Reading the file use r – Read mode

Reading the file and print() all data in the console.

f = open("testFile.txt", "r")
print(f.read())

A complete tutorial of the Reading file in photon must read this tutorial: Python Read File | Python File Open

Delete(Remove) File

The Python delete methods for files and folders.

First, check whether the file or folder exists or not then only delete that file.

We assume have filed in project “cFile.txt.”.

import os
if os.path.exists("cFile.txt"):
  os.remove("cFile.txt")
else:
  print('File does not exists')

For all Delete methods and examples with explanations follow this tutorial: Python Delete File | Remove File

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Python File Handling Examples are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *