Rev 37 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line | 
|---|---|---|---|
| 33 | - | 1 | #! /usr/bin/env python3 | 
| 43 | - | 2 | # ============================================================================= | 
| 3 | # Create | ||
| 4 | # name  : Andre Rikkert de Koe          date:   Apr 2011 | ||
| 5 | # descr : filefind | ||
| 6 | #         This is a demo of programming in the Python language. | ||
| 7 | #         Its part of a project to implement a program to create a directory | ||
| 8 | #         tree similar to the output of the Unix find program. | ||
| 9 | # EUID  : Any user who has read access to all the files being searched for. | ||
| 10 | # run   : interactive | ||
| 11 | # | ||
| 12 | # Changes | ||
| 13 | # name  :                               date: | ||
| 14 | # descr : short description | ||
| 15 | # | ||
| 16 | # ============================================================================= | ||
| 17 | |||
| 2 | ls | 18 | import sys | 
| 19 | import os.path | ||
| 20 | |||
| 21 | # ============================================================================= | ||
| 22 | # Functions | ||
| 23 | # ============================================================================= | ||
| 24 | |||
| 25 | def Usage(mesg): | ||
| 26 | print ("Usage: " + progname + " directory") | ||
| 27 | print (mesg) | ||
| 28 | |||
| 29 | def traverse(directory): | ||
| 30 | curdir = os.getcwd() | ||
| 31 | os.chdir(directory) | ||
| 32 | curdir2 = os.getcwd() | ||
| 33 | files = os.listdir(curdir2) | ||
| 34 | for file in files: | ||
| 35 | pfile = curdir2 + "/" + file | ||
| 36 | if os.path.isdir(file) and not os.path.islink(file): | ||
| 37 | print (pfile) | ||
| 38 | traverse(file) | ||
| 39 |                 else: | ||
| 40 | if os.path.isfile(file) or os.path.islink(file): | ||
| 41 | print (pfile) | ||
| 42 |                         else: | ||
| 43 | print ("WARNING: Could not process file " + pfile) | ||
| 44 | os.chdir(curdir) | ||
| 45 | |||
| 46 | def start_traverse(directory): | ||
| 47 | print (directory) | ||
| 48 | traverse(directory) | ||
| 49 | |||
| 50 | # ============================================================================= | ||
| 51 | # MAIN | ||
| 52 | # ============================================================================= | ||
| 53 | |||
| 54 | progname = os.path.basename(sys.argv[0]) | ||
| 55 | if (len(sys.argv) == 2): | ||
| 56 | directory = sys.argv[1] | ||
| 57 | if os.path.isdir(directory): | ||
| 37 | - | 58 | start_traverse(directory) | 
| 2 | ls | 59 |         else: | 
| 37 | - | 60 | Usage("ERROR: Not a directory : " + directory) | 
| 2 | ls | 61 | else: | 
| 62 | Usage("ERROR: 1 argument expected") |