Funke Python PR 15 Code Snippet
Funke Python PR 15 Code Snippet
try:
with open(filename, 'w') as fileObj:
fileObj.write(text)
print(f"Text written to {filename}")
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
def readFromFile(filename):
try:
with open(filename, 'r') as fileObj:
return fileObj.read()
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
return ""
# Example usage:
writeToFile("my_file.txt", "My name is Funke.\n")
appendToFile("my_file.txt", "I am an aspiring computer scientist\n")
OUTPUT
Contents of 'my_file.txt':
My name is Funke.
I am an aspiring computer scientist