[英]how do i search for specific files by user input
Working with python 2.7 And YES, this is my first question to the most intimidating forum I have ever seen. 使用python 2.7是的,这是我所见过的最令人生畏的论坛的第一个问题。 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. 我希望能够搜索mp3的一个非常大的目录,并告诉我是否在其中输入内容并将其显示在屏幕上。 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.
我需要,对于EG:raw_input(prompt)= Tina Turner。 I want program to look for all files with the name 'Tina Turner' and display them.
我希望程序查找名称为“ Tina Turner”的所有文件并显示它们。 babysteps please, should I use walk or find_all or...I'm already going nuts trying to figure this out on my own.
宝贝步,请问我应该使用walk还是find_all还是...我已经疯了尝试自己解决这个问题。 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.
希望我不会因为这个人生故事而“ P”掉任何一个。 If i don't read anything in a couple of day's, I'll know why.
如果我几天不看任何东西,我会知道为什么。
If you have only a single directory of files then you can use the glob
module. 如果只有一个文件目录,则可以使用
glob
模块。 For this you will have to import glob
为此,您将必须
import glob
That is 那是
for i in glob.glob(filename):
print i
The glob
module is essentially helpful while searching similar patterns. glob
模块在搜索相似模式时本质上很有帮助。
NOTE- The parameter in glob.glob
must be the complete path of the directory. 注:
glob.glob
的参数必须是目录的完整路径。 ie 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
例如,如果文件在桌面中,则必须传递
home/user/Desktop/*Tina Turner*
,其中*
是一个元字符,以匹配名称为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
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. 我将尽力使您的原始代码偏离得尽可能少,尽管对于您的用例,可能按照@BhargavRao的答案所述使用
glob
更好地满足了您的需求。
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:
filename
中的for filename in filenames:
覆盖filename = raw_input
的filename
变量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('/')
walk
的目录设为('.')
,就可以遍历脚本所在的当前目录。如果要遍历整个“硬盘”,只需执行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: 现在,这就是说,您只需使用in运算符即可检查用户输入的字符串是否在文件名中:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.