ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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)
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.