0

i am trying to create a contact list in python. And i am trying to save entered inputs to a txt. and when user wanted to look for a contact from list, i want from program is read the txt file and answer to the user. Thanks

with little research on internet i have found :

with open('list','w') as f:
    f.write()

this technic. But i am new to python thanks for help.

import optparse

def contact_add():
    contact_list = optparse.OptionParser()
    contact_list.add_option("-n","--name",dest="name",help="name to enter")
    contact_list.add_option("-t","--telephone",dest="telephone",help="tel no to enter")

    with open('list','w') as f:
        return f.write(contact_list.name,contact_list.telephone)

The command line :

python contact.py -n hanter -t 0555

This is the output when launched:

Traceback (most recent call last):
  File "contact.py", line 13, in <module>
    contact_add()
  File "contact.py", line 11, in contact_add
    return f.write(contact_list.name,contact_list.telephone)
AttributeError: 'OptionParser' object has no attribute 'name'
  • take a look at the documentation and examples on https://docs.python.org/2/library/optparse.html – Faboor May 06 '19 at 16:13
  • https://stackoverflow.com/questions/4960880/understanding-optionparser – Eular May 06 '19 at 16:14
  • also use argparse instead – Eular May 06 '19 at 16:17
  • I don't think a `return` really makes sense in this instance since you are writing to a file...unless you are trying to return an error code like 1 or 0? You probably want two functions, where one will search your database and the other function will add to it. You can then add an `optparser` argument to specify which one you are doing (and maybe default to one) and the one that writes to the file likely won't return anything. – Reedinationer May 06 '19 at 16:24

2 Answers2

0

You need to call .parse_args() in the argument parser (contact_list in your case) before the parsed arguments become available.

However optparse is deprecated and you should move to argparse which works basically the same.

    (options, arguments) = contact_list.parse_args()
    with open('list','w') as f:
        f.write(options.name, options.telephone)
Faboor
  • 1,365
  • 2
  • 10
  • 23
  • Welcome to Stack Overflow! Please read [what this site is about](https://stackoverflow.com/about) and "[How to answer](https://stackoverflow.com/help/how-to-answer)" before answering a question. – Miroslav Glamuzina May 06 '19 at 19:15
0

contact_list is the OptionParser object. You haven't actually parsed the options yet. Try this:

import optparse

def contact_add():
    contact_list = optparse.OptionParser()
    contact_list.add_option("-n","--name",dest="name",help="name to enter")
    contact_list.add_option("-t","--telephone",dest="telephone",help="tel no to enter")
    parsed_contacts, _ = contact_list.parse_args()

    with open('list','w') as f:
        f.write(parsed_contacts.name+"\t"+parsed_contacts.telephone)

contact_add()
  • contact_list.parse_args() parses the arguments.
  • You don't need the return
  • write takes a single argument; I concatenated the two with a tab

And as the comment by @Eular says, optparse is deprecated, you should use argparse.

Proyag
  • 2,132
  • 13
  • 26