Create A New File Then Write To It

Create A New File And Write To It

# Create a file if it doesn't already exist
with open('file.txt', 'xt') as f:
# Write to the file
f.write('This file now exsits!')
# Close the connection to the file
f.close()

Open The File And Read It

# Open the file
with open('file.txt', 'rt') as f:
# Read the data in the file
# Close the connection to the file
f.close()

View The Contents Of The File

# View the data in the file
data
'This file now exsits!'


Delete The File

# Import the os package
import os

# Delete the file
os.remove('file.txt')