如何通过用户输入搜索特定文件(how do i search for specific files by user input)

编程入门 行业动态 更新时间:2024-10-23 07:17:16
如何通过用户输入搜索特定文件(how do i search for specific files by user input)

使用python 2.7和YES,这是我见过的最令人生畏的论坛的第一个问题。 首先,我不是程序员,但我正在努力学习。

我希望能够通过一个非常大的mp3目录进行搜索,并告诉我我输入的内容是否存在并在屏幕上显示。 而已! 细节:我对编程很新,需要很多细节。 我愿意自己学习这个,需要知道在哪里寻找答案。我一直在这里,谷歌和其他地方环顾四周,但我的项目完全没有信息。

这是我的代码:抱歉把它全部搞定,我是新手,需要了解。

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)

运行此命令将显示所有文件的列表。 我需要,对于EG:raw_input(提示)= Tina Turner。 我希望程序查找名为“Tina Turner”的所有文件并显示它们。 babysteps拜托,我应该使用walk还是find_all或者......我已经疯了,试图自己解决这个问题。 寻找关于我不理解的东西的文档,或者不知道我在寻找什么,是非常令人困惑的。 我是一个菜鸟,所以指出我正确的方向非常感谢。 希望我没有“P”任何关于这个生活故事的解释。 如果我在几天内没有阅读任何内容,我会知道原因。

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.

最满意答案

如果只有一个文件目录,那么可以使用glob模块。 为此你必须import glob

那是

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

在搜索类似模式时, glob模块基本上是有用的。

glob.glob的参数必须是目录的完整路径。 即如果文件在桌面中,您将必须传递home/user/Desktop/*Tina Turner*其中*是元字符,以匹配名称为Tina Turner任何其他文件

或者搜索您可以执行的所有子目录

for dirname, dirnames, filenames in os.walk('.'): for i in glob.glob(dirname+'/*'+filename+'*'): print i

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

更多推荐

本文发布于:2023-08-06 05:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   用户   search   specific   user

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!