如何使用glob访问多个子目录中的文件
我有几个文件存储在名为 Originals 的文件夹中。 Originals 存储在名为 B_Substrate 的文件夹中,B_Substrate 存储在名为 Substrate_Training 的文件夹中,Substrate_Training 存储在名为 Jacob_Images 的文件夹中。
我正在尝试使用 glob 访问 Originals 文件夹中的文件,但无法访问它们。 任何人都可以帮助我了解如何访问多个子目录中的文件
这是我的代码,但它不起作用
import glob
file = '/content/drive/My Drive/Jacob_Images/Substrate_Training/B_Substrate/Originals/*.jpg'
for f in glob.glob(file):
print(f)
I have several files stored in a folder called Originals. Originals is stored in a folder called B_Substrate, B_Substrate is stored in a folder called Substrate_Training and Substrate_Training is stored in a folder called Jacob_Images.
I am trying to access the files in the folder Originals using glob but unable to access them.
Can anyone help me understand how to access files in multiple sub directories
This is my code but it doesn't work
import glob
file = '/content/drive/My Drive/Jacob_Images/Substrate_Training/B_Substrate/Originals/*.jpg'
for f in glob.glob(file):
print(f)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要在路径本身中指定
.jpg
,而是在glob()
函数中指定它。因此,您可以尝试以下操作:或者您可以使用下面的代码,借用这篇帖子中的代码。
你的代码也有效。我确信问题出在您提供的路径上。
使用您的方式:
使用我的方式:
Instead of specifying
.jpg
within the path itself, specify it inglob()
function. So, you can try this:Or you can use this code below borrowing code from this post.
Your code also works. I am certain the issue is with the path you are providing.
Using your way:
Using my way: