I'm having the hardest time figuring out how my latest homework assignment is supposed to work. Basically we have to write a program that creates a list containing names. The list will then be sorted, written to an output file, and searched. We get the names from a text file that is provided to us. It must contain 4 functions. Here's what I have so far.

def main():
    name_list()
    print_list()
    write_output()
    search_list()
    
def name_list():
    infile = open('names.txt', 'r')
    names = infile.readlines()
    infile.close()
    index = 0
    while index < len(names):
        names[index] = names[index].rstrip('\n')
        index += 1
    return names    

def print_list():
    names = [infile]
    print 'Original order:', names
    names.sort()
    print 'Sorted order:', names
    
    
def write_output():    
    outfile = open('names.txt', 'w')
    outfile.write(names)
    outfile.close()
    
def search_list():
    search = input ('Enter last name, first name:')
    if search in text_file:
        print 
    else: 
        print 
    
main()

After reading the book I feel pretty good about my first function. Although that could be wrong, too. I need to return the list of names to the main function then pass the names_list to the remaining functions. The search_list function needs to say whether the input name is in the list and what its index is in the list.

The problem is I keep getting an error for no such file or directory. Anyone have a clue as to why it wont read the list I was provided. I know the codes a mess but I copied the code from the book by itself to see if it would read it and it still wouldn't work. That's like the first step in the program so until I figure that out I can't proceed.

Thanks for any help you can offer.

Recommended Answers

All 8 Replies

Could you post the exact error? That can help a lot.

As well there are a few things you need to address.

def name_list():
    infile = open('names.txt', 'r')
    names = infile.readlines()
    infile.close()
    index = 0
    while index < len(names):
        names[index] = names[index].rstrip('\n')
        index += 1
    return names

When you have return names it doesn't actually do anything as in the main function you do not store the returned value in a variable. So in that case you should do something more like

def main():
    names = name_list()
    print_list()
    write_output()
    search_list()

Now the returned value will be stored in names..

well hopefully that helps a little :)

Member Avatar for masterofpuppets

hi,
have you checked whether the 'names.txt' file is in the current directory you are working in. If it is not you can either move it there or specify the full path of the file :)
second, I agree with Paul Thompson, even if the program opens the file you would get an error like

>>> 

Traceback (most recent call last):
  File "C:/Documents and Settings/jori/Desktop/New Folder/t.py", line 36, in <module>
    main()
  File "C:/Documents and Settings/jori/Desktop/New Folder/t.py", line 3, in main
    print_list()
  File "C:/Documents and Settings/jori/Desktop/New Folder/t.py", line 18, in print_list
    names = [infile]
NameError: global name 'infile' is not defined
>>>

because infile is not defined for the print function. Do something like this:

def main():
    names = name_list()
    print_list( names )
##    ...
##    other stuff

def name_list():
    infile = open( 'names.txt', 'r' )
    names = infile.readlines()
    infile.close()
    index = 0
    while index < len(names):
        names[index] = names[index].rstrip('\n')
        index += 1
    return names

def print_list( n ):
    print 'Original order:', n
    n.sort()
    print 'Sorted order:', n
 
main()

This is the error I get. How would I get the names file into the right directory?

Traceback (most recent call last):
File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", line 42, in <module>
File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", line 8, in main
File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", line 14, in name_list
IOError: [Errno 2] No such file or directory: 'names.txt'
>>>

Okay so you need to just copy the names file into

C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\

I don't think I did it right because I get the same error. I copied the file directly into the Wing IDE 101 3.2\src\debug\tserver folder. Probably did it wrong but I've never done any of this before.

Okkay. Then what i would do is rather than just saying

open("names.txt")

I would do the full address of where i put the file so

open(r"C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\names.txt")

And that should find it then :)

Made some baby steps on this program. Now I get an error that function object is not iterable. This is the first time I've ever gotten that error message. I am guessing that it has something to do with the sorting of the list names.sort(). That's just my guess though, and I am obviously not very good at this. Anyone care to tell me what I did wrong?

def main():
    names = name_list()
    print_list(names)
    write_output(names)
    search_list(names)
    
def name_list():
    infile = open('names.txt', 'r')
    names = infile.readlines()
    infile.close()
    
    index = 0
    while index < len(names):
        names[index] = names[index].rstrip('\n')
        index += 1
    return names    

def print_list(names):
    print 'Here are the names in the original order:'
    for names in name_list:
        print names
    names.sort()
    print 'Here are the names sorted:'
    for names in name_list:
        print names
    
    
def write_output():    
    outfile = open('names.txt', 'w')
    outfile.write(written)
    outfile.close()
    
def search_list():
    search = input ('Enter last name, first name:')
    name_index = name.index(it)    
    if search in names:
        print 'Name is in list', it
    else: 
        print 'Name not found'
    
main()

I assume your error is here ...

def print_list(names):
    print 'Here are the names in the original order:'
    for names in name_list:  # <--------
        print names
    names.sort()
    print 'Here are the names sorted:'
    for names in name_list:  # <-------
        print names

Change it to ...

def print_list(names):
    print 'Here are the names in the original order:'
    for name in names:
        print name
    names.sort()
    print 'Here are the names sorted:'
    for name in names:
        print name
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.