Help with first script please. files, directories, autocomplete

Discussion in 'Python' started by simonharrison@fastmail.co.uk, Oct 7, 2006.

  1. Guest

    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.
    , Oct 7, 2006
    #1
    1. Advertising

  2. Guest

    Forgot to mention I'm using python 2.5 on windows xp.
    , Oct 7, 2006
    #2
    1. Advertising

  3. Rainy Guest

    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
    Rainy, Oct 7, 2006
    #3
  4. 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
    Gabriel Genellina, Oct 10, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Finkel
    Replies:
    0
    Views:
    483
    Joel Finkel
    Sep 12, 2003
  2. Jeffry van de Vuurst
    Replies:
    2
    Views:
    512
    Jeffry van de Vuurst
    Jul 30, 2003
  3. =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=

    Multiple bin-directories with virtual directories?

    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=, Nov 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    812
    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=
    Nov 9, 2004
  4. KK
    Replies:
    2
    Views:
    523
    Big Brian
    Oct 14, 2003
  5. Adam Petrie
    Replies:
    8
    Views:
    293
    Adam Petrie
    Oct 11, 2004
Loading...

Share This Page