Confusing Error when Reading from a File in Python
Question: I'm having a problem opening the `names.txt` file. I have checked that I am in
the correct directory. Below is my code:

    
    
    import os
    print(os.getcwd())
    def alpha_sort():
        infile = open('names', 'r')
        string = infile.read()
        string = string.replace('"','')
        name_list = string.split(',')
        name_list.sort()
        infile.close()
        return 0
    alpha_sort()
    

And the error I got:

> FileNotFoundError: [Errno 2] No such file or directory: 'names'

Any ideas on what I'm doing wrong?


Answer: You mention in your question body that the file is "names.txt", however your
code shows you trying to open a file called "names" (without the ".txt"
extension). (Extensions are part of filenames.)

Try this instead:

    
    
    infile = open('names.txt', 'r')
    

