1

Working with python 2.7 And YES, this is my first question to the most intimidating forum I have ever seen. First off, I am not a programmer, but I am trying to learn.

I want to be able to search through a very large dir of mp3's and tell me if what I type in is there and to display it on screen. that's it! Details: I am very new to programming and need much detail. I am willing to learn this on my own, need to know where to look for answer.I have been looking around on here, Google and other places but no info on my project exactly.

This is my code: sorry for putting it all up, I'm new and need to understand.

from sys import argv
from os import walk
import os

prompt = ':)'


print ("Can I help you with anything today?")
filename = raw_input(prompt)


print ("Looking for %r .... This might take a sec.") % filename
#This is where i get stumped, should I use this or something else?
#don't know how to put userinput into search
for dirname, dirnames, filenames in os.walk('.'):
    # print path to all subdirectories first.
    for subdirname in dirnames:
        print os.path.join(dirname, subdirname)

    # print path to all filenames.
    for filename in filenames:
        print os.path.join(dirname, filename)

print (" That's all i got")
print ("Hit ENTER to exit")
raw_input(prompt)

Running this will display a list of all files. I need, For EG: raw_input(prompt) = Tina Turner. I want program to look for all files with the name 'Tina Turner' and display them. babysteps please, should I use walk or find_all or...I'm already going nuts trying to figure this out on my own. Looking for documentation on something I don't understand, or don't know what I'm looking for, is very confusing. I am a noob so pointing me in the correct direction is greatly appreciated. Hope I didn't "P" any 1 off with this life story of an explanation. If i don't read anything in a couple of day's, I'll know why.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Dave Reid
  • 31
  • 1
  • 9

2 Answers2

3

If you have only a single directory of files then you can use the glob module. For this you will have to import glob

That is

 for i in glob.glob(filename):
       print i

The glob module is essentially helpful while searching similar patterns.

NOTE- The parameter in glob.glob must be the complete path of the directory. i.e If the file is in Desktop the you will have to pass home/user/Desktop/*Tina Turner* where * is a metacharacter to match any other file with the name Tina Turner

Or to search all subdirectories you can do

for dirname, dirnames, filenames in os.walk('.'):
     for i in glob.glob(dirname+'/*'+filename+'*'):
           print i
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • script will be placed in root. I will need to search sub-dir as some point. Can i see an eg: of that? – Dave Reid Dec 26 '14 at 19:41
  • @DaveReid Did you find the answer to your problem? – Bhargav Rao Dec 27 '14 at 10:28
  • I have tried both methods but no success with either of them. I am still looking around (Books, Internet, Friend....yes I have 1) for more info. I cant get it to search for what I'm typing in at prompt. i can bring up all files but not just the one I want. – Dave Reid Dec 27 '14 at 16:06
  • @DaveReid Look at this edit. Even if you are doomed to not get it, you will get the output :) (Coz I've used metachars) – Bhargav Rao Dec 27 '14 at 16:17
  • 1
    I can not thank you enough Bhargav Rao. Thank you for your time and effort. I will be back for more(INFO) Now to make it loop so we can ask for more files. Fun! – Dave Reid Dec 27 '14 at 16:46
1

I'm gonna try to deviate as little as possible from your original code although for your use-case, probably using glob as explained in @BhargavRao's answer suits your needs better.

As far as I understand, I see a couple of thingies that need to be taken into consideration:

  1. You're overwriting the filename variable in filename = raw_input with the for filename in filenames:
  2. By making the directory to walk be ('.'), you're walking the current directory where the script is in. If you want to walk the whole "hard drive", you can just do walk('/')

Now, this said, you could just use the in operator to check whether the string that the user entered is in the file's name:

import os

prompt = ':)'


print ("Can I help you with anything today?")
filename = raw_input(prompt)


print ("Looking for %r .... This might take a sec.") % filename
#This is where i get stumped, should I use this or something else?
#don't know how to put userinput into search
for dname, _, fnames in os.walk('.'):
    # print path to all filenames.
    for fname in fnames:
        if filename in fname:
            print "FOUND: %s" % os.path.abspath(os.path.join(dname, fname))

print (" That's all i got")
print ("Hit ENTER to exit")
raw_input(prompt)
Community
  • 1
  • 1
Savir
  • 17,568
  • 15
  • 82
  • 136
  • where would one find doc on why its giving me 1 letter on the output of the file name? eg: user_input = readme result line 1 /home/blah/blah/r result line 2 /home/blah/blah/e – Dave Reid Dec 26 '14 at 21:26
  • That sounds like you're iterating through the `"readme"`string... Try this in your terminal: `for letter in "readme": print letter` Probably you stored the user's input into a `filename` variable, and you're doing something like `for foo in filename`. At that point, `filename` is an string, and the `for` gives you letter by letter (that's my guess, without more information) – Savir Dec 26 '14 at 21:30