Here is how to create a new file in Python.

import os 
# Create a new text file and add some content
with open('newfile.txt', 'w') as file:
    file.write("I am the file content!") 
# Check if the file has been created


['', 'newfile.txt']


The open() function is used to open files in Python if they exist. If not, a new file with the given name will be created. 

Related HowTos
Schedule a Python script for execution at a specific time every day
Store Python passwords securely on Windows, Mac, and Linux
Do dictionary comprehension
Do list comprehension
Create a virtual environment
Merge two lists
Extract items from two different lists into one list
Check if a text file is empty
Randomly select an item from a list
Generate a random integer
Break a while loop
Create a pandas DataFrame from a dictionary
Create a pandas DataFrame from a list
Get the last item of a list
Delete a column from a pandas dataframe
Access a column of a pandas dataframe
Create a class
Make a webpage request
Get the first two characters of a string
Recommended Course

Learn Flask development and learn to build cool apps with our premium Python course on Udemy.

Mailing List

We will keep you Python-motivated if you subscribe below.