CT240 lab10


http://geminga.it.nuigalway.ie/~gettrick/courses/CT240/labs/l10.html




In this lab, you should run PYTHON from the command line (i.e. do not run IDLE). In Windows, click on the start menu, and select "Visual Studio 2008 Command Prompt" [or: select firstly "Microsoft Visual Studio" followed by "Visual Studio 2008 Command Prompt" in a sub-menu]. Alternatively select the menus MAIN --> UTILITIES --> Command Line. Then you should type in cd C:\Python25. Now in your current folder you have the PYTHON executable. So you can type

  1. python
  2. python file.py to run the program file.py
  3. python file.py arg1 arg2 arg3 ... to run the program file.py with some command line arguments.
Note that in practice file.py may be C:\file.py or F:\file.py or U:\file.py etc. depending on where you have the program (e.g. on USB stick). Note also: If you can't find PYTHON executable: Go to SEARCH in the main menu, and type in python in the search.
Write a program in PYTHON that searches a (text) file for a given string. The user should be allowed to put the name of the file, and the name of the string to be searched for, as command line arguments. Your program should also use 3 flags:
  1. -h (meaning help), which does not run the search but just prints out a help message.
  2. -s which searches for a string that occurs anywhere (i.e. as part of a word or on its own).
  3. -w which searches for a word, i.e. a string surrounded by whitespace (space, tab, newline,...)
Moreover, your program should output to the user, the line numbers in the text file where the string occurs.

An example follows: suppose the text file is called carol.txt, containing the 4 lines:

Good King Wenceslas looked out
on the Feast of Stephen,
When the snow lay round about,
deep and crisp and even.
and suppose you name your PYTHON program search.py: Then
  1. python search.py -s out carol.txt should return the numbers 1 and 3 (where the string "out" occurs)
  2. python search.py -s ee carol.txt should return the number 4
  3. python search.py -w out carol.txt should return the number 1 only.
You should use the find() method of the string object to do the search.

© NUI, Galway