Trying to write a python app that reads phone numbers from a file
Question: I am quite new to Python. At the moment I am trying to write a script that can
read a `.txt` file that contains a bunch of data, and pull out phone numbers
that are in the format of `(xxx)xxx-xxxx`.

Here is my current attempt, but its not working at all and I am lost:

    
    
    #import argv
    from sys import argv
    
    script, filename = argv
    
    txt_file = open(filename)
    indata = txt_file.read()
    
    #confirm to the user what file is being open
    print "Opening %r" % filename
    
    #create a loop to read through the file
    for line, in line enumerate(indata):
        if line == "(" + \w\w\w\ + ")" + \w\w\w "-" + \w\w\w
        print line
    
    txt_file.close()
    

Can anyone provide me with a suggestion on how to make this work?


Answer: First of all:

    
    
    import sys
    filename = sys.argv[1] #Grabs first argument
    
    #confirm to the user what file is being open
    print "Opening %r" % filename
    
    with open(filename,'rb') as txt_file: #Opens the file
        for line in txt_file:  #Reads the file line by line.
            if ####    #checks for ...
    

Sys.argv is a list, so the first argument passed is sys.argv[1]. You don't
need script because you don't use it. Don't use read() because that stores the
entire file as a list and all you need to do is check each line. Use with for
good measures when you're opening/writing/closing files. with closes the file
as you exit the block.

I'll need to see what your text file looks like to finish.

