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.")

Answers


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.


Need Your Help

Add-Type error Meta File could not be found

c# powershell

I've create a basic C# class that implements Microsoft.Data.Schema.ScriptDom and Microsoft.Data.Schema.ScriptDom.Sql interface. These two assemblies are part of Visual Studio Database Edition (VSDB...

How can we add hover effects by hovering something else, like given below? (HTML, CSS3)

html css css3

I made two divs and what I want, is to use pure CSS and add something like when I hover the first div the other div change its background.