How to "Create, Write, Read and Delete" File on Python
Today we are going to show you how to:
1 - create and write
2 - read
3 - delete
file in Python programming language
1 - create and write
print("File Name:")
fileName = input();
try:
file = open(fileName, "w");
textToWrite = input("Please Write the content:")
file.write(textToWrite)
print("Content Successfully write to file!")
except FileExistsError:
print("File Already Exist");
2 - read this file in python
print("File Name:")
fileName = input();
try:
file = open(fileName, "r");
textFromFile = file.read()
print("File Text:");
print(textFromFile)
except FileNotFoundError:
print("File Not Found");
3 - Remove or Delete this file
import os
print("Enter File Name To Delete:")
fileName = input()
try:
os.remove(fileName)
print("File Deleted!")
except FileNotFoundError:
print("File Already Deleted")
Full Video:
No comments