Reputation: 31
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.
Upvotes: 1
Views: 2263
Reputation: 52071
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
Upvotes: 3
Reputation: 18418
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:
filename
variable in filename = raw_input
with the for filename in filenames:
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)
Upvotes: 1