Download Introduction to Python

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
New York University
School of Continuing and Professional Studies
Division of Programs in Information Technology
Introduction to Python
Homework, Session 8
Note on exceptions: try: blocks should go around as few lines as possible, for two reasons:
it will help visually identify where the exception is expected, and it will help avoid a potential
error occurring unexpectedly elsewhere in your code and being handled by your try: block.
If you find yourself writing multiple try/except blocks that seem to lengthen your code and
make it less readable, consider combining multiple exceptions into one try: block (again, as
long as try: block doesn't include too many extraneous lines). See the slides for options in
combining exceptions.
EXERCISES
Exceptions
Ex. 8.1 Allow your Program to Accept Arguments. Accept two arguments at the command
line, using the default list object sys.argv, a list that is automatically available after
you execute the statement import sys. Sum the values and print out a formatted
string with an "addition" formula. (As mentioned in the slides, the first argument is at
sys.argv[1], the second at sys.argv[2], etc.)
Expected output (2 program runs; I have named my solution script addargs.py -the $ represents the comand line):
$ python addargs.py 5 19
5 + 19 = 24
$ python addargs.py 100 4
100 + 4 = 104
Ex. 8.2 Validate Arguments. Extend the above program to validate user input. Using try:
and except:, trap an IndexError exception if one of the two arguments is missing and have the program exit() with a Usage: string if the exception occurs (see output
and note below).
Exceptions: trap an exception for missing arguments (IndexError when reading
from sys.argv). (Remember to place as little code as possible in your try: block.)
If possible, print the program name in the Usage string (as shown below) using
sys.argv[0].
Expected output (3 program runs; I have named my solution script
validate_addargs.py -- the $ represents the comand line):
$ python validate_addargs.py
Usage: validate_addargs.py [int1] [int2]
$ python validate_addargs.py 5
Usage: validate_addargs.py [int1] [int2]
$ python validate_addargs.py 5 hello
Usage: validate_addargs.py [int1] [int2]
$ python validate_addargs.py 5 10
5 + 10 = 15
File and Directory Input / Output
Note on files and directories: anytime we ask Python to ask the OS to find a file or directory
(through os.listdir(), os.path.isfile(), os.path.getsize(), etc.), the OS is starting from the
present working directory (pwd), which is the directory from which we are running our Python
program. If the OS can't find this file or directory in the pwd, it will not look elsewhere.
The straightforward way to find a file or directory is to use the absolute pathname, which is
the full location of the file within the filesystem. In Unix/Linux/Mac, it begins with a forward
slash, e.g. /Users/dblaikie/thisdir/myfile.txt; and in Windows it usually begins with C:\: as
in C:\Documents\ and\ Settings\dblaikie\thisdir\myfile.txt, or
C:\Users\dblaikie\thisdir\myfile.txt.
To find the absolute path of any file, go to the directory where it is listed, and type pwd at the
prompt. Add the filename to this path and you should have the absolute path. You can
confirm this is the case with one of the following commands: ls [pathname] (that's "ell ess")
on Unix/Linux/Mac, or dir [pathname] on Windows.
Ex. 8.3 os.path.getsize(). Pick a file in the same directory as your Python script (it can
even be this very same Python script!). Write a script that prints the size of the file.
import os
filename = 'myfile.txt'
print os.path.getsize(filename)
# use the name of an existing file
where filename is the name of the file (the filename can be hard-coded in your
script). The program should print out the size of the file in bytes.
Expected output (will vary depending on file chosen):
myfile.txt:
150 bytes
Ex. 8.4 Validate filename. Continuing the previous program, take a filename from the user
through the command line. Use os.path.isfile() to see if the submitted file is a file in
the current directory. If it is a file, then print the filename and size. If it is not a file,
then print an error message.
Expected output (2 program runs; I have named my solution script
validate_filename.py -- the $ represents the comand line):
$ python validate_filename.py myfile.txt
myfile.txt: 84 bytes
$ python validate_filename.py yourfile.txt
error: yourfile.txt is not a file in this directory
Ex. 8.5 List a Directory. Accept a string argument that is the pathname of a directory. Print
out all items in the directory listing using os.listdir(). Trap the exception that would
result from an unreadable directory (i.e., if not exist or no permissions. To
determine this exception, try to read a directory that doesn't exist, and note the
exception that results). Using os.path.isfile(listing) and os.path.isdir(listing),
where listing is one of the items returned from listdir(), identify whether the listing is
a file or directory.
Note that while listdir returns filenames, it doesn't return file paths. So if the
directory you're listing isn't in the same directory as your Python program, you'll
need the whole path. You can construct a whole path and test a file this way:
filepath = os.path.join(dir, filename)
so you can do a file test this way:
if os.path.isfile(os.path.join(dir, filename)):
Exceptions: trap exception for missing argument (IndexError when trying to access
sys.argv[1]) and unreadable directory (OSError when attempting to read directory
using os.listdir()).
Expected output (of course depends on the directory you supply):
please enter a directory:
test1.txt (file)
test2.txt (file)
test3dir (dir)
test
Ex. 8.6 List files and sizes in another directory. Continuing the previous exercise, output the
file name and byte size (using os.path.getsize()) of each file.
please enter a directory:
test1.txt (file): 84
test2.txt (file): 672
test3dir (dir): 136
test
HOMEWORK
8.1
Directory Grep. grep is a Unix utility that does a text search of a file or files. It prints
each line of a group of files that contains the search text. This program will do the
same work as grep. Accept two arguments: the name of an existing directory, and a
search string. Loop through each plaintext file in the directory, printing every line of
each file that contains the search string, preceded by the filename and the line
number where the string was found (see output).
Hint: remember that in can be used to find one string within another string.
Exceptions: trap exceptions for missing arguments and for unreadable directory.
Expected output (this is based on the bible/ directory unzipped from the bible.zip file
found in the source data page on the class website. In the example below, the (614)
number is the line number of the Acts.txt file. (The directory is just the location on my
own system.)
$ python dirgrep.py /Users/David/bible/ Roman
Acts.txt (614): 21 And teach customs, which are not lawful for us to
receive, neither to observe, being Romans.
Acts.txt (630): 37 But Paul said unto them, They have beaten us openly
uncondemned, being Romans, and have cast [us] into prison; and now do they
thrust us out privily? nay verily; but let them come themselves and fetch us
out.
# output continues...
Acts.txt (1050): 17 And it came to pass, that after three days Paul called
the chief of the Jews together: and when they were come together, he said
unto them, Men [and] brethren, though I have committed nothing against the
people, or customs of our fathers, yet was I delivered prisoner from
Jerusalem into the hands of the Romans.
John.txt (550): 48 If we let him thus alone, all [men] will believe on him:
and the Romans shall come and take away both our place and nation.
Romans.txt (467):
<? Written to the Romans from Corinthus, [and sent]
by Phebe servant of the church at Cenchrea.>
8.2
A watched directory. Accept one argument: the pathname of an existing directory.
Create a program that stays running and continually watches the directory to see if
any files have been added or removed. The program first lists and stores the files
found in the directory at program start, and subsequently "polls" (re-checks) the
directory files to see if any changes have been made. The checking can be done
every 5 seconds or so.
To pause the program so it can wait before checking the directory again, use the
following code:
import time
# at top of script with other imports
time.sleep(5)
To test, keep your program running, and then manually go into the directory and
create and remove files to see the program's output reflect the changes.
Hint: your program will run for an indefinite period, looping endlessly and checking the
contents of the directory continuously. Use a while(True) loop to do this. The loop
will not stop looping until the program is terminated using Ctrl-C.
Exceptions: trap exceptions for missing argument and unreadable directory.
Expected output (will of course vary based on directory and add/remove events):
$ python watchdir.py my_files_dir/
# time passes as the program polls (periodically reads) the directory
# at some point, we add two files to the directory (you can do this
manually)
file1.txt added
file2.txt added
# time passes as the program continues polling the directory
# at some point, we remove one of the files
file2.txt removed
# time passes... we add two more files
file3.txt added
file4.txt added
# time passes... we remove one of the files
file3.txt removed
SUPPLEMENTARY (EXTRA CREDIT) EXERCISES
8.3
Largest files in a directory using os.walk. Accept two arguments: the name of an
existing directory, and the number of files to display. Use os.walk to go through an
entire directory tree (review slide explaining this function). Store the names of a
directory's files (the directory is submitted as an argument on the command line)
paired with each file's size. Print the number of files requested, sorted by size, largest
to smallest. Print out the filenames along with their paths.
Exceptions: trap exceptions for missing arguments and if the second argument is not
an integer. (os.walk does not raise an error if the directory is invalid; choose your
own method of noting this error.)
Expected output (will of course vary based on arguments):
$ python largestfiles.py '/Users/david/python' 5
9339 homework_4_plaintext
8366 homework_4_outline.txt
6148 .DS_Store
564 ff_highestval.py
513 watched.py