finding file

Discussion in 'Python' started by su, Jun 5, 2006.

  1. su

    su Guest

    Hi , I am trying to understand myself with some basic programs in
    python, I have written a small script to search for core files in the
    current dir. but when i tried to execute, it is searching the core
    files in the subdir also. could someone help me on how can i restrict
    my code to search the file in the current dir only

    import os, os.path
    import re
    def core_finder(arg, dir, files):
    for file in files:
    path = os.path.join (dir, file)
    if re.search("core.*", path):
    print "found"
    print path


    os.path.walk('.', core_finder, 0)
    su, Jun 5, 2006
    #1
    1. Advertising

  2. su wrote:
    > could someone help me on how can i restrict
    > my code to search the file in the current dir only


    Use os.listdir().

    -- Christoph
    Christoph Zwerschke, Jun 5, 2006
    #2
    1. Advertising

  3. su

    K.S.Sreeram Guest

    su wrote:
    > import os, os.path
    > import re
    > def core_finder(arg, dir, files):
    > for file in files:
    > path = os.path.join (dir, file)
    > if re.search("core.*", path):
    > print "found"
    > print path
    >
    >
    > os.path.walk('.', core_finder, 0)


    Here's a simpler solution:

    import glob
    filenames = glob.glob( 'core*' )

    In case you want the full path...

    import os
    filepaths = [os.path.join(os.getcwd(),f) for f in filenames]

    Regards
    Sreeram


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2.2 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFEhCdmrgn0plK5qqURAqgIAKCdAAneKUsbTc4tpTL+X6TQWSBzfQCfVQa1
    k9JyMzmD3f1uTRNLA2zggIk=
    =tNvm
    -----END PGP SIGNATURE-----
    K.S.Sreeram, Jun 5, 2006
    #3
  4. su

    K.S.Sreeram Guest

    K.S.Sreeram wrote:
    > filepaths = [os.path.join(os.getcwd(),f) for f in filenames]


    you can use os.path.abspath....

    filepaths = [os.path.abspath(f) for f in filenames]


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2.2 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFEhCmwrgn0plK5qqURAvo+AKCjVNGDdMnN9yKZtQxQ1qUf53wYwgCgkl+i
    S4zuxFikP3TkMQtiARjyN0M=
    =UWnD
    -----END PGP SIGNATURE-----
    K.S.Sreeram, Jun 5, 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. vivienne wykes
    Replies:
    1
    Views:
    450
    Andrew Thompson
    Aug 3, 2004
  2. Abs
    Replies:
    1
    Views:
    531
    Alex Kizub
    Jan 8, 2005
  3. Benz
    Replies:
    3
    Views:
    459
    Mark Murphy
    Feb 3, 2005
  4. steve
    Replies:
    5
    Views:
    495
    Owen Jacobson
    Jun 13, 2005
  5. Luc The Perverse

    Finding Duplicate File Objects

    Luc The Perverse, Feb 25, 2006, in forum: Java
    Replies:
    1
    Views:
    374
    Roedy Green
    Feb 25, 2006
Loading...

Share This Page