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 print(os.listdir())
['main.py', '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.
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.