Help with first script please. files, directories, autocomplete

Rainy ak at silmarill.org
Sat Oct 7 15:34:06 EDT 2006


simonharrison at fastmail.co.uk wrote:
> Hello everyone. Hopefully someone can point me in the right direction
> here. I'm wanting to write a script to open microsoft word and adobe
> pdf documents . Here is a little background:
>
> At the company where I work (an inspection firm) all reports of
> inspections are saved as word files. A particular file may contain many
> reports named like this; 12345A-F.doc. This file contains six reports.
> Most inspections also require a technique which is saved as a pdf. The
> pdf filename is the identification of the part being inspected.
>
> My command line script will work like this: The user is asked whether
> they are searching for a technique or a report
>
> > Find (c)ertificate or (t)echnique
>
> if they press 'c' they are then asked to enter certificate number.
> Using the code below, no enter is needed
>
> import msvcrt
> print "Find (t)echnique or (c)ertificate: "
> ch = msvcrt.getch()
> if ch == 't':
>   print "Enter technique number: "
> elif ch == 'c':
>   print "Enter certificate number: "
> else:
>   print 'command not understood.'
> raw_input()
>
> Obviously I will need to wrap this into a function. What I need to know
> how to do is save the two directories where the files are stored. if
> 'c' is selected I want to use that as the directory to search. same for
> techniques. What is the best way to do this? I would also like to have
> the text autocomplete after maybe three characters, is this possible?
> Am I correct in thinking all files would have to be stored in a list
> for this to work?
>
> As you can tell I am new to programming. I don't want someone to write
> this script for me, just give me some pointers to get going (maybe a
> tutorial on the net). Unless someone really wants to write it of
> course!
>
> Many thanks and sorry for the long post.

You can store the dir name as a variable:

>>> d = 'c:\home'
>>> from os import *
>>> listdir(d)
['.Config.pm.swp', '.run.bat.swp', 'AHK scripts', 'Archive',
'Config.pm', 'Docs', 'Images', 'Links', 'Music', 'Projects', 'Python
programs', 'run.bat', 'Share', 'Torrent']

As you see files are already in a list if you use listdir function.

You can autocomplete by getting each character, running through the
list and comparing using startswith() function:

>>> l = listdir(d)
>>> m = [m for m in l if m.startswith('A')]
>>> m
['AHK scripts', 'Archive']

Then you can check how many matches you got. If you get one, print it
and ask for Enter to finalize the choice.

You might want to read tutorial on python.org. You also might want to
buy a python book, or read any number of other tutorials online if you
don't want to spend money right now. Your questions are kind of basic,
I don't want to discourage you but as you go along you will run into
many other things and it's not practical to ask every time and wait for
the answer (although people here are glad to help). 

 -Rainy




More information about the Python-list mailing list