473,396 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

finding file

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)

Jun 5 '06 #1
3 1413
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
Jun 5 '06 #2
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+X6 TQWSBzfQCfVQa1
k9JyMzmD3f1uTRNLA2zggIk=
=tNvm
-----END PGP SIGNATURE-----

Jun 5 '06 #3
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+AKCjVNGDdMnN9yKZtQxQ1q Uf53wYwgCgkl+i
S4zuxFikP3TkMQtiARjyN0M=
=UWnD
-----END PGP SIGNATURE-----

Jun 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Noam Dekers | last post by:
Hi all, I would like to find a word stored in a text file. Structure: I have one file named keyWords.txt that stores some key words I'm interested in finding. In addition I also have a file...
1
by: D. Alvarado | last post by:
Hello, Does anyone have a PHP 4 one-liner (or two-liner) for extracing a file from a directory in which I know the word "footer" is guaranteed to be in the file name, I know the precise directory...
13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
15
by: Benjamin Rutt | last post by:
Are there any C tools that can find redundant #includes in a project, so as to shorten compile time? Of course, eliminating single #includes by hand and determining if the recompile fails is one...
8
by: Rick Strahl [MVP] | last post by:
Hi all, I'm building an app that uses the ASP.Net runtime... One problem I've run into is that pages running inside of the runtime are not finding DLLs in the GAC. In fact, if I look at the...
0
by: NSF12345 | last post by:
Iv developed a small program that looks for a file over our network, and copy it to the location of another computer. Im using the "If FileExists("\\oldpc\main share\Folder\file.txt") Then" way of...
1
by: me | last post by:
Hi, Im having a few issues with finding exactly which program may be accessing a certain file. Lets say I need to replace a DLL with a newer version, but I need to make sure its not being...
1
by: Empyrean | last post by:
I'm attempting to make my first program that involves file input, but I'm running into problems finding the .txt file. I placed the file inside the same folder as the rest of the project, but it...
1
by: avik1612 | last post by:
Hi, I have created a program to find text files in a particular directory or folder. and to find a particular word in that files i finding it difficult to put the list in an array and finding...
9
by: richee | last post by:
Hi all, I am new to Perl but see that it offers some fantastic opportunities and am trying to use it more in problem solving but could do with a little help on a problem thats driving me nuts.......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.