-2

Basically I started Python a couple of days ago and wanted to create a program that could read and write files. Problem is I get this error: io.UnsupportedOperation: not writable

choice = input("Open / Create file: ")
if choice == 'Create' or choice == 'create':
    new_file_name = input("Create a name for the file: ")
    print(open(new_file_name, "w"))
    text = input("Type to write to file: \n")
    file2 = open(new_file_name)
    print(file2.write(text))
    print("Reading file...")
    print(open(new_file_name, "r"))
    print(file2.read())
elif choice == 'Open' or choice == 'open':
    filename = input("File name or directory: ")
    file = open(filename)
    open(filename, "r")
    time.sleep(1)
    print("Reading file...")
    time.sleep(1)
    print(file.read())
    choice2 = input("Write to file? Y/N \n")
    if choice2 == 'Y' or choice2 == 'y':
        text2 = input("Type to write to file: ")
        open(filename, "w")
        file = open(filename)
        file.write(text2)
        choice3 = input("Read file? Y/N ")
        if choice3 == 'Y' or choice3 == 'y':
            print(file.read())
tehc
  • 1

1 Answers1

1

Your idea of issuing progress reports from your code is a good one, especially in the beginning stages. But it appears that you don't quite understand the difference between

print(open(new_file_name, "w"))

which is what your code actually does, and

print(f'open("{new_file_name}", "w")')

I believe the second of these is what you meant to do: it prints

open("myfile.txt", "w")

but what your actual code does is to (1) create an open file object for writing, then (2) print its type and memory location to the screen, and finally (3) throw it away.

So the first fix is to take out the print() calls, or at least reduce them to print("step 1") etc until you know how to do it properly.

The second fix is to not respond to the choice of Create by trying to read the file. If the user is creating the file then they are clearly not interested in the contents of any previous version. Your code responds to Create by reading the file, and that seems back-to-front to me, and in general programs should work the way that the average user, for example me, will think intuitive. Here is a correct way to do the Create bit:

choice = input("Open / Create file: ")
if choice == 'Create' or choice == 'create':
    new_file_name = input("Create a name for the file: ")
    with open(new_file_name, "w") as file2:
        file2.write("This is stuff to go into the created file.\n")
else:
    ...

This asks for the name of the file, opens it for writing, then writes some stuff to it.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • Thank you very much for being patient and explaining it very well. I really appreciate it! – tehc Apr 27 '19 at 07:53
  • a small note on that mystery notation: `f'....'` is a "formatted string" that lets you template in information from other variables, so if you had a variable like `name = '...'` then you could do `salute = f'Good morning, {name}'`. In this case, however, since we're not templating anything in, you don't strictly need the `f` version of the string, and just `print('.....')` will work fine. – Mike 'Pomax' Kamermans Apr 27 '19 at 16:14