I am writing code for class and I'm stuck on just one problem. I'm supposed to use the main function to call to a different function called 'file2list' which will take the file (that I brought from main) and convert it to a list. I have everything needed to create the list, I just can't get the file to run. (also I'm new to programming, so there can also be some silly errors)
This is in my main function
#Call file2list
vt=file2list('vt_municipalities.txt')
nh=file2list('nh_municipalities.txt')
And then this is what my file2list function looks like
def file2list(muni_file):
#Create lists
muni_list=[]
#Open files
file=open('muni_file','r')
Basically, how can I bring the .txt file down to file2list? I get the error that muni_file doesn't exist. Thanks!
CodePudding user response:
Your error is that you are trying to open the file with the name muni_file
, as you surrounded it with '
, making it a string. To reference the variable that was passed into the function, remove the quotation marks ('
) surrounding muni_file
.
The file=open
line should look something like this:
file=open(muni_file,'r')
CodePudding user response:
you are passing to open()
a string that contains 'muni_file' as value, to use the parameter that you recieve, you should pass without ' '