Python User Input File Name
I'm trying to write a program that takes a users input for a file name, and then either opens the file to add to it, or prints what is already in the file, so far this is what I have: (I keep getting a syntax error where it tries to print 'file name' is now open) any ideas/tips?
elif choice == 'a': print("You selected 'a', you can now add to your file.") print("File", ui "is now open.")
You are having the following print statement in your elif block
print("File", ui "is now open.")
which is syntactically wrong, since it doesn't have a comma after ui, and you can't concatenate or perform any other operation thus.
Either of the following should correct that
print("File", ui, "is now open.") print("File %s is now open." % ui)
Another problem beyond the one you asked about:
r = open(ui, 'r') print(r) close.ui()
I assume this is meant to print the contents of the file named by ui. But what it will actually do is to print something like <open file 'filename', mode 'r' at 0x10d7556f0>, and then throw an exception once it hits close.ui(). r is the file object; you want to read it, and then close it -- both of these are methods of r:
r = open(ui, 'r') print(r.read()) r.close()
Hmm, I see you edited this out of your question while I was typing. Hope it helps anyway.