Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help with first script please. files, directories, autocomplete

Reply
Thread Tools

Help with first script please. files, directories, autocomplete

 
 
simonharrison@fastmail.co.uk
Guest
Posts: n/a
 
      10-07-2006
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.

 
Reply With Quote
 
 
 
 
simonharrison@fastmail.co.uk
Guest
Posts: n/a
 
      10-07-2006
Forgot to mention I'm using python 2.5 on windows xp.

 
Reply With Quote
 
 
 
 
Rainy
Guest
Posts: n/a
 
      10-07-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      10-10-2006
At Saturday 7/10/2006 16:34, Rainy wrote:

>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']


Note that \ is a escape character, and works fine in this case only
because \h is not a valid sequence.
Use instead 'c:\\home' or r'c:\home' or 'c:/home' (forward slashes
are fine in Windows too)

from ... import * is not the recommended way; use instead:

from os import listdir
listdir(d)

or

import os
os.listdir(d)

--
Gabriel Genellina
Softlab SRL





__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a script from another script and other script does notdo busy wait. Rajat Python 3 01-08-2010 02:05 PM
Enable autocomplete although site set autocomplete to off Frank Dohrmann Firefox 1 03-31-2006 11:28 PM
help with my first project on first job, how to read a strange file, thanks a lot!!!!!!! matt Java 9 10-27-2004 03:32 AM
Need help with autocomplete function Leeon55 Computer Information 1 11-22-2003 07:10 PM
Need help with autocomplete function leeon55 Computer Information 0 11-16-2003 08:27 AM



Advertisments