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.
Learn Flask development and learn to build cool apps with our premium Python course on Udemy.
Subscribe to be the first to know when a new Python course is released.