finding file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • su

    #1

    finding file

    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)

  • Christoph Zwerschke

    #2
    Re: finding file

    su wrote:[color=blue]
    > could someone help me on how can i restrict
    > my code to search the file in the current dir only[/color]

    Use os.listdir().

    -- Christoph

    Comment

    • K.S.Sreeram

      #3
      Re: finding file

      su wrote:[color=blue]
      > 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)[/color]

      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

      iD8DBQFEhCdmrgn 0plK5qqURAqgIAK CdAAneKUsbTc4tp TL+X6TQWSBzfQCf VQa1
      k9JyMzmD3f1uTRN LA2zggIk=
      =tNvm
      -----END PGP SIGNATURE-----

      Comment

      • K.S.Sreeram

        #4
        Re: finding file

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

        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

        iD8DBQFEhCmwrgn 0plK5qqURAvo+AK CjVNGDdMnN9yKZt QxQ1qUf53wYwgCg kl+i
        S4zuxFikP3TkMQt iARjyN0M=
        =UWnD
        -----END PGP SIGNATURE-----

        Comment

        Working...