TypeError:"PosixPath"类型的参数不可迭代 [英] TypeError: argument of type 'PosixPath' is not iterable

查看:86
本文介绍了TypeError:"PosixPath"类型的参数不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图递归遍历文件夹,以将特定文件分配给变量以供以后解析,但是我收到以下错误:

I am trying to recursively iterate through folders to assign a particular file to a variable for later parsing, however I am receiving the following error:

TypeError: argument of type 'PosixPath' is not iterable

Flask version 1.0.2 (not sure if its relevant)
Python version 3.7.3

进口:

from pathlib import Path
from glob import glob

代码:

def parse_info(bundle_path):
    file_list = []
    for filename in Path(bundle_path).glob('**/*.*'):
        file_list.append(filename)

    for elem in file_list:
        if 'uname' in elem:
            print('present')

致电:

parse_info(<some path>)

'print(filename)'确实打印出了该路径中的所有文件,并且所需的搜索字符串出现在该打印输出中,所以我知道它存在,但是只是不确定如何在以后捕获该元素.任何帮助将不胜感激.

The 'print(filename)' does print out all the files in that path and the desired search string is present in that print output, so I know it exists, however just not sure how to capture that element for later. Any help would be appreciated.

推荐答案

在这种情况下,答案是将文件名附加到列表时将其转换为字符串:

The answer in this case was to cast the filename to a string when appending it to the list:

file_list.append(str(filename))

这篇关于TypeError:"PosixPath"类型的参数不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆