递归返回文件的路径名
我需要一个程序,该程序将文件名和文件夹路径名作为参数,并直接或间接搜索文件夹中的文件以及其中包含的任何文件夹。该函数应返回文件的路径名(如果找到);如果在文件夹或文件夹的任何子目录中找不到该文件,则返回 None。该函数必须是递归的。
这是我的代码:
def search(fname, path):
for item in os.listdir(path):
next = os.path.join(path, item)
try:
search(next,fname)
except:
return next
它应该看起来像这样:
>>>search('fileA.txt', 'test')
'test\\fileA.txt'
>>>search('fileB.txt', 'test')
'text\\folder2\\fileB.txt'
等等。 但我只能让我的代码找到 fileA.txt,无论我告诉它寻找什么文件。
我向老师寻求帮助,这是她告诉我的:
**我看到几个问题:
- 您的函数中没有条件来实际检查是否找到该文件。进行更改时请参阅下一期。
- 如果您已经将路径添加到 fname 上,则 fname 将不会与对应的文件匹配。在创建文件的完整路径名之前,您需要检查文件名,否则将无法匹配它。
- 您不对递归调用返回的值执行任何操作。这些应该返回路径或无,并且您不会检查返回的内容。**
i need a program that takes as parameters the name of a file and the pathname of a folder and search for the file in the folder and any folder contained within it, directly or indirectly. The function should return the pathname of the file, if found, or None if the file cannot be found in the folder or in any subdirectory of the folder. The function must be recursive.
here is my code:
def search(fname, path):
for item in os.listdir(path):
next = os.path.join(path, item)
try:
search(next,fname)
except:
return next
it should look like something along the lines of:
>>>search('fileA.txt', 'test')
'test\\fileA.txt'
>>>search('fileB.txt', 'test')
'text\\folder2\\fileB.txt'
ect.
but i can only get my code to find fileA.txt, no matter what file i tell it to look for.
I asked the teacher for some help, this is what she told me:
**I see several problems:
- You don’t have a condition in your function that actually checks to see if you’ve found the file. See the next issue when working on changing this.
- The fname won’t match the file that corresponds to it if you’ve already added the path onto it. You need to check for the file name before you create the full pathname for the file, or you won’t have a way to match it.
- You don’t do anything with the values that are returned from the recursive call. Those should be returning either paths or None, and you’re not checking what’s coming back.**
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的递归元素混淆了。尝试
搜索(fname,下一个)
。另外,正如 Brendan 提到的,您应该使用
if/else
,而不是try/ except
,因为这里不会抛出任何错误。最后,您似乎没有基本情况(例如,没有更多的目录可以遍历),即终止递归并防止无限循环/递归的最终条件。
Your recursion elements are mixed up. Try
search(fname, next)
.Also, as mentioned by Brendan, you should be using
if/else
, nottry/except
, since no errors are being thrown here.And last of all, you don't seem to have a base case (e.g., no further directories to traverse), a final condition which will terminate recursion and prevent infinite looping/recursion.
您可以使用 os.walk() 来执行此操作:
You could use os.walk() to do this:
您需要一个递归终止条件。考虑一下函数应该在什么条件下返回以及在什么条件下它应该继续查找。然后编写一个
if/else
块来测试这些条件。如果返回条件成立,则返回正确的值。否则,返回递归调用的结果。换句话说,它应该看起来像这样(示意性地):You need a recursion termination condition. Think about the conditions under which the function should return and the conditions under which it should keep looking. Then write an
if/else
block to test for those conditions. If the return condition holds, return the correct value. Otherwise, return the result of a recursive call. In other words, it should look like this (schematically):