LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2013, 11:19 AM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Rep: Reputation: 60
Search for a File Python


I am back at it with Python and have run into a little stupid hurdle. My goal is to simply search for the GeoIP.dat database and add the path to a couple of variables. So for example:

Code:
geopath=os.system('find /usr/share -iname GeoIP.dat')
geobase = pygeoip.GeoIP(geopath, pygeoip.MEMORY_CACHE)
which will load the GeoIP database so I can begin adding addition logic to my script. The issue I am having is when I simply execute using ipython:

Code:
geopath=os.system('find /usr/share -iname GeoIP.dat')
geopath
Out[15]: 0
it returns "0" in which I assume mean successful. So my stupid question is how do I see what was returned? Why doesnt it return the results instead of a "0"?

Last edited by metallica1973; 10-24-2013 at 11:24 AM.
 
Old 10-24-2013, 11:29 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,236

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
I think you want "subprocess.check_output". See here:

http://andymckay.github.io/presentat...index.html#/37

Or, you could do it more pythonically:

Code:
import os


def find_file(root, name):

    for root, dirs, files in os.walk(root):
        for filename in files:
            if filename.lower() == name:
                return os.path.join(root, name)

geopath = find_file('/usr/share', 'GeoIP.dat')
geobase = pygeoip.GeoIP(geopath, pygeoip.MEMORY_CACHE)
 
1 members found this post helpful.
Old 10-24-2013, 11:40 AM   #3
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
You should not use os.system in that way. Reading output of find line-by-line is not error-prof. Furthermore, there is no need to spawn a new process to look for files with given name since this can be done in Python. What you need is os.walk:
Code:
import os

geopaths = []
for dirname, _, filenames in os.walk('/ush/share'):
    for filename in filenames:
        if filename.lower() == 'geoip.dat':
            geopaths.append(os.path.join(dirname, filename))
Quote:
Originally Posted by dugan View Post
Code:
def find_file(root, name):
    for root, dirs, files in os.walk(root):
        for filename in files:
            if filename.lower() == name:
                return os.path.join(root, name)

geopath = find_file('/usr/share', 'GeoIP.dat')
Either pass “geoip.dat” or do not use “lower()” in the function.

Last edited by mina86; 10-24-2013 at 12:17 PM. Reason: Updating as I did not notice it was -iname at first.
 
1 members found this post helpful.
Old 10-24-2013, 11:48 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,236

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by mina86 View Post
Either pass “geoip.dat” or do not use “lower()” in the function.
Revising:

Code:
def find_file(root, name):

    for root, dirs, files in os.walk(root):
        for filename in files:
            if filename.lower() == name.lower():
                return os.path.join(root, name)
That should give you the same results as "find -iname" now.
 
Old 10-24-2013, 12:18 PM   #5
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by dugan View Post
Code:
if filename.lower() == name.lower():
Better yet, add “name = name.lower()” at the beginning of the function.
Quote:
Originally Posted by dugan View Post
Code:
return os.path.join(root, name)
This should read: “return os.path.join(root, filename)”.
 
Old 10-24-2013, 01:23 PM   #6
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
Awesome Many Thanks I will test this stuff and give you an update.

Last edited by metallica1973; 10-24-2013 at 01:26 PM.
 
Old 10-24-2013, 02:17 PM   #7
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
I know I will recieve a scolding but the easiest method is Dugan:

Code:
caca = subprocess.check_output(['sudo', 'find', '/usr/share', '-iname', 'GeoIP.dat'])
[sudo] password for me: 

In [75]: caca
Out[75]: '/usr/share/GeoIP/GeoIP.dat\n'
If I can remove the \n, this will satisfy, I believe this is what I need and the less code the better. Ok let me have it.

this did the trick:
Code:
mod = caca[:-1].strip()
'/usr/share/GeoIP/GeoIP.dat'
long story short:
Code:
caca = subprocess.check_output(['sudo', 'find', '/usr/share', '-iname', 'GeoIP.dat'])
mod = caca[:-1].strip()
geobase = pygeoip.GeoIP(mod, pygeoip.MEMORY_CACHE)

Last edited by metallica1973; 10-24-2013 at 02:49 PM.
 
Old 10-24-2013, 02:37 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,236

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by metallica1973 View Post
If I can remove the \n
Code:
caca.strip()
(There are other ways, but this IMHO makes the most sense).

Last edited by dugan; 10-24-2013 at 02:54 PM.
 
1 members found this post helpful.
Old 10-25-2013, 03:42 AM   #9
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by metallica1973 View Post
I know I will recieve a scolding but the easiest method is Dugan:
As long as you are aware it's incorrect and use it by yourself, I don't care what you're using.

Last edited by mina86; 10-29-2013 at 04:06 AM. Reason: Toned down a little, not to sound offensive.
 
1 members found this post helpful.
Old 10-25-2013, 04:10 PM   #10
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
Wink

ouch
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
search in python for a text at runtime and send output to a file shwhooda Programming 1 06-29-2012 04:34 AM
python os.walk file search question (un expected duplicate thread) bloodyscript Programming 2 09-26-2011 10:52 AM
python os.walk file search question [SOLVED] bloodyscript Programming 3 09-24-2011 05:09 PM
Python: search for string in a list or file chess Programming 3 08-22-2007 04:22 PM
Python search and replace Accordion Programming 1 02-22-2005 07:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration