Program that can find a find a file for you ?

Discussion in 'Python' started by Peter Hansen, Sep 29, 2004.

  1. Peter Hansen

    Peter Hansen Guest

    Greetings.

    Im trying to write a program that can be run from the command line.
    If I want to search for example after a file with the ending .pdf, I should
    be able to write in the command line:
    python name of my program / the libary to search and what kind of file it
    is example a .pdf file
    So if my program name was test.py and the library name was library1 and the
    test type i wanted to find was, a .pdf file
    I should write python test.py /library1 .pdf

    I have to use some of the code below, but im very lost, I know that I have
    use os, but not quite sure. Does any have any ideas.....

    Thanks all

    ***
    # -*- coding: utf-8 -*-
    import sys

    def useArgument(arg):


    if __name__=="__main__":
    """
    python argtest.py arg1 arg2 arg3
    """
    if len(sys.argv)!=4:
    sys.exit("Error, not the right amout of arguments")

    for arg in sys.argv:
    useArgument(arg)

    ***
     
    Peter Hansen, Sep 29, 2004
    #1
    1. Advertising

  2. Peter Hansen wrote:
    > Im trying to write a program that can be run from the command line.
    > If I want to search for example after a file with the ending .pdf, I should
    > be able to write in the command line:
    > python name of my program / the libary to search and what kind of file it
    > is example a .pdf file
    > So if my program name was test.py and the library name was library1 and the
    > test type i wanted to find was, a .pdf file
    > I should write python test.py /library1 .pdf
    >
    > I have to use some of the code below, but im very lost, I know that I have
    > use os, but not quite sure. Does any have any ideas.....


    You should investigate using the standard library module
    getopt, or perhaps optparse (a newer one), as they are
    designed to handle command line arguments with ease. Doing
    it yourself is probably a waste of time.

    (By the way, would you consider changing your "From:"
    and "Reply-To:" headers to include an initial or something?

    Having two Peter Hansens around here could be confusing,
    and I've been a frequent contributor so you might find
    many people thinking you're me and sending you hate mail
    or something, and you wouldn't know why... If you don't
    want to do that, I will. I just hope your middle name
    doesn't begin with "L"...)

    -Peter L Hansen (the regular)
     
    The Regular Peter Hansen, Sep 29, 2004
    #2
    1. Advertising

  3. Am Wed, 29 Sep 2004 11:25:39 +0200 schrieb Peter Hansen:

    > Greetings.
    >
    > Im trying to write a program that can be run from the command line.
    > If I want to search for example after a file with the ending .pdf, I should
    > be able to write in the command line:
    > python name of my program / the libary to search and what kind of file it
    > is example a .pdf file
    > So if my program name was test.py and the library name was library1 and the
    > test type i wanted to find was, a .pdf file
    > I should write python test.py /library1 .pdf


    Hi,

    This is something the "find" command does in a unix environment.

    Here is my solution:

    You could use this:
    find.py your_path 'library1.*\.pdf$'

    #!/usr/bin/env python
    # -*- coding: iso-8859-1 -*-


    # Python Imports
    import os
    import re
    import sys

    def usage():
    print """Usage: %s path regex
    Print all files or directories with match the regex.

    See this URL for the syntax of the regular expressions
    http://docs.python.org/lib/re-syntax.html

    """ % (os.path.basename(sys.argv[0]))

    def visit(regex, dirname, names):
    for name in names:
    p=os.path.join(dirname, name)
    if regex.search(p):
    print p
    def main():
    if len(sys.argv)!=3:
    usage()
    sys.exit(1)
    path=sys.argv[1]
    regex=sys.argv[2]
    os.chdir(path)
    regex=re.compile(regex)
    os.path.walk(".", visit, regex)

    if __name__=="__main__":
    main()
     
    Thomas Guettler, Sep 29, 2004
    #3
  4. Peter Hansen

    Peter..... Guest

    Hi again all.
    I allmost did it, just need the line to run the program now, any ideas, my
    head hurts, cant think anymore..... Thanks for your help

    import sys
    import os.path
    import os.dir
    a = sys.argv[2]
    b = sys.argv[3]

    def func (bib, end):
    c = os.path.dirlist(bib)

    for x in c:
    d = os.dir.split(x)
    if [1] = end
    print (bib, end)

    if __name__=="__main__":





    "Thomas Guettler" <> wrote in message
    news:p...
    > Am Wed, 29 Sep 2004 11:25:39 +0200 schrieb Peter Hansen:
    >
    >> Greetings.
    >>
    >> Im trying to write a program that can be run from the command line.
    >> If I want to search for example after a file with the ending .pdf, I
    >> should
    >> be able to write in the command line:
    >> python name of my program / the libary to search and what kind of file
    >> it
    >> is example a .pdf file
    >> So if my program name was test.py and the library name was library1 and
    >> the
    >> test type i wanted to find was, a .pdf file
    >> I should write python test.py /library1 .pdf

    >
    > Hi,
    >
    > This is something the "find" command does in a unix environment.
    >
    > Here is my solution:
    >
    > You could use this:
    > find.py your_path 'library1.*\.pdf$'
    >
    > #!/usr/bin/env python
    > # -*- coding: iso-8859-1 -*-
    >
    >
    > # Python Imports
    > import os
    > import re
    > import sys
    >
    > def usage():
    > print """Usage: %s path regex
    > Print all files or directories with match the regex.
    >
    > See this URL for the syntax of the regular expressions
    > http://docs.python.org/lib/re-syntax.html
    >
    > """ % (os.path.basename(sys.argv[0]))
    >
    > def visit(regex, dirname, names):
    > for name in names:
    > p=os.path.join(dirname, name)
    > if regex.search(p):
    > print p
    > def main():
    > if len(sys.argv)!=3:
    > usage()
    > sys.exit(1)
    > path=sys.argv[1]
    > regex=sys.argv[2]
    > os.chdir(path)
    > regex=re.compile(regex)
    > os.path.walk(".", visit, regex)
    >
    > if __name__=="__main__":
    > main()
    >
     
    Peter....., Sep 29, 2004
    #4
  5. Peter Hansen

    Dan Perl Guest

    You didn't almost do it, you are still very far from it. The code you've
    written so far is full of mistakes and I'm really not sure even what you are
    trying to do.

    Instead of writing the whole program in one shot, try to write just
    something very small, try it, make it work and then add one more small
    thing. For instance, try to run a script with only the first 5 lines that
    you wrote and you will find a problem already. Fix that and then add the
    function with only its first statement. Invoke the function and you will
    find another problem. Fix that.

    And so on.

    I'm curious though. Is this maybe an assignment for a course that you are
    taking?

    Dan

    "Peter....." <> wrote in message
    news:415abff2$0$13728$...
    > Hi again all.
    > I allmost did it, just need the line to run the program now, any ideas, my
    > head hurts, cant think anymore..... Thanks for your help
    >
    > import sys
    > import os.path
    > import os.dir
    > a = sys.argv[2]
    > b = sys.argv[3]
    >
    > def func (bib, end):
    > c = os.path.dirlist(bib)
    >
    > for x in c:
    > d = os.dir.split(x)
    > if [1] = end
    > print (bib, end)
    >
    > if __name__=="__main__":
    >
    >
    >
    >
    >
    > "Thomas Guettler" <> wrote in message
    > news:p...
    >> Am Wed, 29 Sep 2004 11:25:39 +0200 schrieb Peter Hansen:
    >>
    >>> Greetings.
    >>>
    >>> Im trying to write a program that can be run from the command line.
    >>> If I want to search for example after a file with the ending .pdf, I
    >>> should
    >>> be able to write in the command line:
    >>> python name of my program / the libary to search and what kind of file
    >>> it
    >>> is example a .pdf file
    >>> So if my program name was test.py and the library name was library1 and
    >>> the
    >>> test type i wanted to find was, a .pdf file
    >>> I should write python test.py /library1 .pdf

    >>
    >> Hi,
    >>
    >> This is something the "find" command does in a unix environment.
    >>
    >> Here is my solution:
    >>
    >> You could use this:
    >> find.py your_path 'library1.*\.pdf$'
    >>
    >> #!/usr/bin/env python
    >> # -*- coding: iso-8859-1 -*-
    >>
    >>
    >> # Python Imports
    >> import os
    >> import re
    >> import sys
    >>
    >> def usage():
    >> print """Usage: %s path regex
    >> Print all files or directories with match the regex.
    >>
    >> See this URL for the syntax of the regular expressions
    >> http://docs.python.org/lib/re-syntax.html
    >>
    >> """ % (os.path.basename(sys.argv[0]))
    >>
    >> def visit(regex, dirname, names):
    >> for name in names:
    >> p=os.path.join(dirname, name)
    >> if regex.search(p):
    >> print p
    >> def main():
    >> if len(sys.argv)!=3:
    >> usage()
    >> sys.exit(1)
    >> path=sys.argv[1]
    >> regex=sys.argv[2]
    >> os.chdir(path)
    >> regex=re.compile(regex)
    >> os.path.walk(".", visit, regex)
    >>
    >> if __name__=="__main__":
    >> main()
    >>

    >
    >
     
    Dan Perl, Sep 29, 2004
    #5
  6. Peter Hansen

    Steve Guest

    Hi Peter,

    On Wed, 29 Sep 2004 11:25:39 +0200, Peter Hansen <> wrote:
    > Im trying to write a program that can be run from the command line.
    > If I want to search for example after a file with the ending .pdf, I should
    > be able to write in the command line:
    > python name of my program / the libary to search and what kind of file it
    > is example a .pdf file


    I had to do something like this sometime back so I wrote up a general
    purpose script that would look for certain types of files and call a
    python function, passing the filename as argument to the function.
    This function could be any thing that you would care to define (My
    script incidentally just rename file with *ill formed* names). This is
    roughly the equivalent doing this using the 'find' unix command:

    $ find -name "*.ext" -exec (some python function) {} ';'

    You can find the function at the ASPN cookbook site:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/300411

    Hope you find it useful.

    Regards
    Steve
     
    Steve, Sep 29, 2004
    #6
  7. Peter Hansen

    Peter..... Guest

    Thanks for your input.

    I solved the last few lines and corrected the errors that was in it.
    And it does work.
    Stay happy.....



    "Steve" <> wrote in message
    news:...
    > Hi Peter,
    >
    > On Wed, 29 Sep 2004 11:25:39 +0200, Peter Hansen <>
    > wrote:
    >> Im trying to write a program that can be run from the command line.
    >> If I want to search for example after a file with the ending .pdf, I
    >> should
    >> be able to write in the command line:
    >> python name of my program / the libary to search and what kind of file
    >> it
    >> is example a .pdf file

    >
    > I had to do something like this sometime back so I wrote up a general
    > purpose script that would look for certain types of files and call a
    > python function, passing the filename as argument to the function.
    > This function could be any thing that you would care to define (My
    > script incidentally just rename file with *ill formed* names). This is
    > roughly the equivalent doing this using the 'find' unix command:
    >
    > $ find -name "*.ext" -exec (some python function) {} ';'
    >
    > You can find the function at the ASPN cookbook site:
    >
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/300411
    >
    > Hope you find it useful.
    >
    > Regards
    > Steve
     
    Peter....., Sep 29, 2004
    #7
    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. Doris Cox
    Replies:
    0
    Views:
    545
    Doris Cox
    Dec 2, 2003
  2. Dennis
    Replies:
    0
    Views:
    693
    Dennis
    Dec 2, 2003
  3. Richard Dixson
    Replies:
    1
    Views:
    515
    Joe Fallon
    May 18, 2004
  4. Dennis
    Replies:
    0
    Views:
    448
    Dennis
    Dec 2, 2003
  5. Jim Holder

    How do you find the header file you need?

    Jim Holder, Dec 19, 2003, in forum: C Programming
    Replies:
    5
    Views:
    434
    Randy Howard
    Dec 21, 2003
Loading...

Share This Page