Exercise 4.2

Objectives:

  • Learn how to perform common file system operations

  • Learn how to launch subprocesses

  • Writing a program that accepts command line options

Files Created: None

Files Modified: report.py

In this exercise, we’re simply going to look at some of the modules used for performing common tasks on the system such as working with files, directories, launching subprocesses, and so forth. These are tasks that might be normally written in shell scripts.

(a) Manipulating filenames

Start by putting a filename in a string:

>>> filename = 'Data/portfolio.csv'
>>>

In many programs, you might need to perform various actions involving file and pathnames. More often than not, you use the os.path module for this. Let’s try some operations:

>>> import os.path
>>> os.path.basename(filename)      # Get basic filename
'portfolio.csv'
>>> os.path.dirname(filename)       # Get the path
'Data'
>>> os.path.abspath(filename)       # Get absolute path (output varies)
'/Users/beazley/Desktop/practical-python/Data/portfolio.csv'
>>>

Let’s probe the file a bit:

>>> os.path.exists(filename)
True
>>> os.path.isfile(filename)
True
>>> os.path.isdir(filename)
False
>>> os.path.getsize(filename)
126L
>>> os.path.getmtime(filename)
1248789706.0
>>> import time
>>> time.ctime(os.path.getmtime(filename))
'Sun Jan 19 05:34:52 2014'
>>>

Let’s change the filename so that it’s in a different directory.

>>> os.path.join('NewData', os.path.basename(filename))
'NewData/portfolio.csv'
>>>

(b) Finding files on the file system

The library module glob has a function glob() that returns a list of filenames that match a given shell pattern. For example, to get a list of all .py files in the current directory, you do this:

>>> import glob
>>> glob.glob('*.py')
[ 'bounce.py', 'hello.py', 'mortgage.py', 'sears.py', ... ]
>>>

If you need to search through many directories, use the os.walk() function. Try this example which prints all Python files under the current working directory:

>>> import os
>>> for path, dirs, files in os.walk('.'):
        for filename in files:
             if filename.endswith('.py'):
                  print os.path.join(path, filename)

... look at the output ...
>>>

(c) Running a Subprocess

Use the subprocess module to run the external command "netstat -a" and collect the output in a string. This should work on both Unix and Windows.

>>> import subprocess
>>> out = subprocess.check_output(['netstat','-a'])
>>> print out
... you should see the output of netstat -a here ...
>>>

Note: If you’re using Python 2.6 or earlier, you’ll need to use this instead:

>>> import subprocess
>>> p = subprocess.Popen(['netstat','-a'],stdout=subprocess.PIPE)
>>> out = p.stdout.read()
>>> print out
... you should see the output of netstat -a here ...
>>>

(d) Command Line Options

Many real-world Python programs run as programs from the command line. Modify your report.py program so that it accepts the two filenames on the command line. For example:

bash % python report.py Data/portfolio.csv Data/prices.csv
      Name     Shares      Price     Change
---------- ---------- ---------- ----------
        AA        100       9.22     -22.98
       IBM         50     106.28      15.18
       CAT        150      35.46     -47.98
      MSFT        200      20.89     -30.34
        GE         95      13.48     -26.89
      MSFT         50      20.89     -44.21
       IBM        100     106.28      35.84
bash %
Links