Reputation: 51
I'm not sure how to word the question, and I'm not finding anything in searches.
I have a variable, and want to pass it into f.open()
as the name of the file to open.
The goal is to type a file name into foo, then it clears the files.
foo = str(raw_input('enter a filename '))
bar = foo
f = open('foo', 'w')
f.write("")
f.close()
I've tried using open(foo, 'w') and replacing it with 'bar', but neither seems to work. Any suggestions?
Upvotes: 0
Views: 73
Reputation: 85612
Remove the ''
from 'foo'
:
f = open('foo', 'w')
Like this:
f = open(foo, 'w')
'foo'
is a literal string and foo
is a variable name that holds the file name the user entered.
Upvotes: 2
Reputation: 5396
f = open(bar, 'w')
Just pass the variable name (without quotes of course).
Additionally, you don't need the line bar = foo
. You are already storing the filename as foo
with your first line.
So... if you remove bar = foo
it would be f = open(foo, 'w')
Upvotes: 1