简体   繁体   English

如何通过用户输入搜索特定文件

[英]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: 据我了解,我认为需要考虑一些问题:

  1. You're overwriting the filename variable in filename = raw_input with the for filename in filenames: 您正在用filename中的for filename in filenames:覆盖filename = raw_inputfilename变量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('/') 通过将要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.

相关问题 如何在文本文件中搜索特定列以获取用户输入 - How can I search a specific column in text file for a user input 搜索特定单词的用户输入 - Search User Input for Specific Words 如何将用户输入存储为多个条目,然后允许用户搜索他们创建的项目? - How do I store the user input as multiple entries, and then allow the user to search for the items they created? 如何使我的Redditbot目标搜索特定用户,评论和/或线程而不是整个subreddit? - How do I make my Redditbot target-search a specific user, comment, and/or thread instead of an entire subreddit? 如何让用户输入以在 django/python 中搜索内容? - How do I make a input from the user to search for something in django/python? 如何将用户输入放入文本小部件中并使用 Python 使用 tkinter 在日志文件中进行搜索 - How can I put user input inside text widget and do search in Log file with tkinter using Python 如何从搜索栏中获取用户输入以显示在页面中? Django - How do I get user input from search bar to display in a page? Django 如何在 python 中搜索 SQL 列表以检查用户名是否与用户输入匹配? - How do I search a SQL List inside of python to check if the username matches a user input? 如何在Python 2.7中将用户输入限制为特定的整数,并跟踪异常? - How do I limit user input to specific integers, and keep track of exceptions, in Python 2.7? 如何根据用户输入将字符串打印到特定位置的字符串中? - How do I print a string into a string in a specific location depending on user input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM