How to use Python to Create Files?
Tags: python, programming, tutorial
Published: 2021-08-27 12:03:02

In this short tutorial, we learn how to use Python to create a file. We also look at the write, read, and append file handling methods.

This tutorial is a part of our initiative at [Flexiple](https://flexiple.com/), to write short curated tutorials around often used or interesting concepts.

### Table of contents
- [Why use Python to Create files?](#why-use-python-to-create-files) 
- [How to create files in Python](#how-to-create-files-in-python) 
- [File handling methods](#file-handling-methods) 
- [Closing thoughts](#closing-thoughts) 

## Why use Python to Create files?
Like most programming languages, Python too supports file handling. However, since Python treats files as text or binary, handling files is much easier. The most used file handling methods are to create, read and write files. 

In this tutorial, we largely focus on how to use Python to create a file as this is the first step to file handling. However, we do introduce you to how you could read and write files. 

## How to create files in Python:
To create a file in Python we use the in-built `open()` function. There are other methods that utilize the OS module in Python, however, a few methods would require root permission. Hence to standardize this tutorial I have only used the `open()` method. 

### Syntax of open():
```python
open(file, access mode)
```

### Parameters:
The `open()` methods require two parameters, 
- `File` - Required, the name or path of the file. 
- `Access mode` - Access modes govern the type of operations you would be performing. 

Since we are trying to create a file, the access mode we would use is “x”. I’ve added a list of all the access modes below. 

### Code & Explanation:
```python
f = open("myfirstfile.txt", "x")
```

In the above code, we have used the `open()` to create a file, since we have not specified a path, the file is created in the same directory as the script. 

In case the file already exists, Python returns a `FileExistsError`. Try running the same code twice and you will receive this error. 

## File handling methods:
Apart from the create method, I’d like to give you an introduction about the read, write and append methods. I’ve added below a list of all the methods along with their access modes. 

- **Read-only (‘r’)**: This method is used to read an existing file. In case the file does not exist, a `FileNotFoundError` is returned. 
- **Read and Write (‘r+’)**: This method is used to read and write a file, and similar to Read-only a `FileNotFoundError` is returned if the file does not exist.
- **Write Only (‘w’)**: This method can be used to open and write to a file. In case a file does not exist, the file is created. And in case it does, the content of the file is overwritten.
- **Append (‘a’)**: This method is used to add content to a file. The handler is positioned at the bottom of the file and hence the content is written at the bottom of the file. The method also creates a file in case it does not exist.

##Closing thoughts:
As you may have noticed, the write and append methods can also be used to create files. However, I would recommend you familiarize yourself with the create methods first before you start to use other methods in Python to create files. 

This is because the write method overwrites the existing data. Although the append methods do not overwrite data both these methods are not very user-friendly while creating files. Hence do practice creating files first before you start using the other methods to do the same.

Do let me know your thoughts in the comments section below. :)