Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8362165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:52:44+00:00 2026-06-09T11:52:44+00:00

I have been trying to learn how to use sys.argv properly, while calling an

  • 0

I have been trying to learn how to use sys.argv properly, while calling an executable file from the command line.

I wanted to have the functions results print to the command line when passing the filename and argument on the command line but, I get a TypeError.

So far I have:

#! /usr/bin/env python
import mechanize
from BeautifulSoup import BeautifulSoup
import sys

def dictionary(word):
    br = mechanize.Browser()
    response = br.open('http://www.dictionary.reference.com')
    br.select_form(nr=0)
    br.form['q'] = sys.argv
    br.submit()
    definition = BeautifulSoup(br.response().read())
    trans = definition.findAll('td',{'class':'td3n2'})
    fin = [i.text for i in trans]
    query = {}
    for i in fin:
        query[fin.index(i)] = i
    return query

print dictionary(sys.argv)

When I call this:

./this_file.py 'pass'

I am left with this error message:

Traceback (most recent call last):
  File "./hot.py", line 20, in <module>
    print dictionary(sys.argv)    
  File "./hot.py", line 10, in dictionary
    br.form['q'] = sys.argv
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_form.py", line 2782, in __setitem__
    control.value = value
  File "/usr/local/lib/python2.7/dist-packages/mechanize/_form.py", line 1217, in __setattr__
    raise TypeError("must assign a string")
TypeError: must assign a string
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:52:45+00:00Added an answer on June 9, 2026 at 11:52 am

    With

    br.form['q'] = sys.argv

    you are assigning a list of strings here instead of a string.

    >>> type(sys.argv)
    <type 'list'>
    >>> type(sys.argv[0])
    <type 'str'>
    >>>
    

    You want to identify a specific string to assign via an index.

    Most likely it will be be index 1 given what you have in your post (and since index 0 is the name of the script). So perhaps

    br.form['q'] = sys.argv[1]

    will do for you. Of course it could be another index too, depending on your particular application/needs.

    Note as @Dougal observes in a helpful comment below, the function parameter word in the function is not being used. You are calling your dictionary function sending it sys.argv and then ought to refer to word inside the function. The type doesn’t change only the name that you refer to the command line args inside your function. The idea of word is good as it avoids the use of global variables. If you refer to use globals (not really encouraged) then removing word is recommended as it will be confusing to have it there).

    So your statement should really read

    br.form['q'] = word[1]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to learn to use github, it has been fine until
I have been trying to learn to use Ogre3D and have gotten to the
I have been trying to learn from reading tutorials online and stuff but I
I have been trying to learn a bit more about delegates and lambdas while
I've been trying to learn to to use Android SDK and have encountered my
I have been trying to learn how to build jQuery plugins, so I'm still
I have been trying to learn python over the last couple of months and
I have been trying to learn Erlang and have been running into some problems
I have been trying to learn a cross platform language with a fast learning
Hi I have been trying to learn Javascript using codeacademy.com and I have reached

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.