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 7830769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:24:11+00:00 2026-06-02T11:24:11+00:00

I have this code: #! /usr/bin/python import sys, string def findAll(search, fh): count =

  • 0

I have this code:

#! /usr/bin/python
import sys, string

def findAll(search, fh):
    count = 0
    for line in fh:
        count += 1
        if line.find(search) != -1:
            print "%3d: %s"%(count, line.rstrip())
    return count

search = raw_input("Enter string to be found: ")
filename = raw_input("Enter filename: ")
fh = open(filename, "rU")
findAll(search, fh)

My professor recommended I write this code and incorporate “improved usage”.
I’m confused as to how but she recommended that

  1. I modify the program by commenting out the raw_input() statements, then adding statements to check if the program is invoked with fewer than 2 arguments and if so to print 'Usage: findstring.py string filename. The code takes strings and locates them in a file.

  2. I use the filename command line argument from sys.argv to open the file and prepare for an input/output error (IOError) to occur. Then to use a try-except block to encode what to do if opening the file works or not.
    If the opening fails, I print 'Error: cannot open findstring.py where findstring.py is also the considered the text file.

To be honest… I was so busy writing down her suggestions that I had no idea how to do many of the things she recommended. Can someone help improve this code? I’m confused and I don’t know how to do this. My prof did say that the code would run, but I don’t know how to modify it.

  • 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-02T11:24:13+00:00Added an answer on June 2, 2026 at 11:24 am

    For improved usage, try using the argparse module. It makes taking command-line options easier.

    http://docs.python.org/library/argparse.html#module-argparse

    A code sample from the above link reads:

    import argparse
    
    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument('integers', metavar='N', type=int, nargs='+',
                       help='an integer for the accumulator')
    parser.add_argument('--sum', dest='accumulate', action='store_const',
                       const=sum, default=max,
                       help='sum the integers (default: find the max)')
    
    args = parser.parse_args()
    print args.accumulate(args.integers)
    

    Now think about how you might modify this sample for your assignment. You need to take strings (search term, filename) instead of integers.

    For the try/except block, remember that the code to handle an error goes in the except portion of the block. That is, you might consider showing an error message in the except block.

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

Sidebar

Related Questions

I have this code: #!/usr/local/bin/python """ USAGE: apache_logs.py """ import sys import os if
here is my code #!/usr/bin/python # -*- coding:utf-8 -*- import sys import pysvn def
I have the following code: #!/usr/bin/env python import sys from PyQt4 import QtGui, QtCore
With this code, urllib2 make a GET request: #!/usr/bin/python import urllib2 req = urllib2.Request('http://www.google.fr')
I have a Python code that uses Paramiko. #!/usr/bin/env python import paramiko username =
Consider the code below: #!/usr/bin/env python from PyQt4 import QtCore, QtGui import os,sys class
I have opened a shelve using the following code: #!/usr/bin/python import shelve #Module:Shelve is
#!/usr/bin/python2.4 import logging import sys import doctest def foo(x): >>> foo (0) 0 print
The code #!/usr/bin/env python import MySQLdb print Content-Type: text/html print print <html><head><title>Books</title></head> print <body>
I have this code for monitoring a webcam with python and pygtk. The question

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.