在Python中查找文件的确切目录
有一个包含文件名列表的数据框:在Python中查找文件的确切目录,python,pandas,file,directory,python-os,Python,Pandas,File,Directory,Python Os,有一个包含文件名列表的数据框: Files 3003.txt 3000.txt 还有三个目录,我想在其中搜索这些文件: dirnames=[ '/Users/User/Desktop/base/reports1', '/Users/User/Desktop/base/reports2', '/Users/User/Desktop/base/reports3'] 这样做: for dirname in dirnames: for f in os.listdir(
Files
3003.txt
3000.txt
还有三个目录,我想在其中搜索这些文件:
dirnames=[
'/Users/User/Desktop/base/reports1',
'/Users/User/Desktop/base/reports2',
'/Users/User/Desktop/base/reports3']
这样做:
for dirname in dirnames:
for f in os.listdir(dirname):
with open (os.path.join(dirname,f), "r", encoding="latin-1") as infile:
if f==df.Files:
text = infile.read()
print(f,dirname)
给出了错误
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
如何正确重写if条件或以另一种方式创建for循环
谢谢 我想您需要添加Files.items。然后将这些文件与由此生成的迭代器进行比较。”“文件”指向panda系列。如果df.Files.items中的f是一个系列,那么我该怎么做呢?问题是df.Files是一个系列,所以当你做f==df.Files时,你会得到一个像True-False这样的系列。。。在没有任何附加信息的情况下,定义该序列的单一真值是不明确的。您真正想要的是f==df.Files.any或df.Files.tolist中的f