Python file overwriting script I am fairly new to Python and I'm trying to make a script that does three basic things: prints the contents of a file, overwrites it with your new lines, and prints the new file. I rewrote the script with functions and was looking for critiques. Is this a good use of functions? What would you do different?

print("Type the name of the file for overwriting.")
file = input("&gt;")
x = open(file)
print("The contents are currently: \n", x.read())
print("Enter in the new lines")
l1 = input("&gt;")
l2 = input("&gt;")
l3 = input("&gt;")
x.close()

y = open(file, 'w')

print("Entering new lines.")

y.write(l1 + '\n' + l2 + '\n' + l3)
y.close()

z = open(file).read()
print("The new contents are:")
print(z)


Script with functions:

def open_and_read(x):
    print(open(x).read())

def nl(x):
    z.write(x + '\n')

temp = "&gt;"  
print("Type the name of the file for overwriting.")
file = input(temp)
print(f"The current content of {file} is:")
open_and_read(file)

z = open(file, 'w')

print("Enter in three new lines of text:")
l1 = input(temp)
l2 = input(temp)
l3 = input(temp)

nl(l1)
nl(l2)
nl(l3)

z.close()

print("The new contents are:")
open_and_read(file)
First of all, you should definitely switch to using with context manager which is gonna safely and automatically close the opened file handler.

Also, you need to improve your variable names, l1, l2, l3 or z are not descriptive.

And, you may use .writelines() to write a list of strings each of them on a separate line.

In the end, you might get something along these lines:

print("Type the name of the file for overwriting.")
file_name = input("&gt;")

with open(file_name) as input_file:
    print("The contents are currently: \n", input_file.read())
    print("Enter in the new lines")

lines = [input("&gt;") for _ in range(3)]

print("Entering new lines.")
with open(file_name, 'w') as output_file:
    output_file.writelines(lines)

with open(file_name) as input_file:
    print("The new contents are:")
    print(input_file.read())