0% found this document useful (0 votes)
9 views

Funke Python PR 15 Code Snippet

All in one doc I just needed a ldf

Uploaded by

Ov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Funke Python PR 15 Code Snippet

All in one doc I just needed a ldf

Uploaded by

Ov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

def writeToFile(filename, text):

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 appendToFile(filename, text):


try:
with open(filename, 'a') as fileObj:
fileObj.write(text)
print(f"Text appended 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")

# Read the contents of the file


file_contents = readFromFile("my_file.txt")
print("\nContents of 'my_file.txt':")
print(file_contents)

OUTPUT

Text written to my_file.txt


Text appended to my_file.txt

Contents of 'my_file.txt':
My name is Funke.
I am an aspiring computer scientist

You might also like