finding file

S

su

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)
 
K

K.S.Sreeram

su said:
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

K.S.Sreeram

K.S.Sreeram said:
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-----
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top