简体   繁体   English

为什么 glob.glob 返回一个空列表?

[英]why is glob.glob returning an empty list?

Please i need help.请我需要帮助。 I am using windows.我正在使用窗户。 I am getting an empty list when I run this glob code.当我运行这个 glob 代码时,我得到一个空列表。 my ipynb file is inside the code folder, and I have 4 different folders inside the train and test folders that serve as labels for my image task.我的 ipynb 文件位于代码文件夹中,并且我在 train 和 test 文件夹中有 4 个不同的文件夹,它们用作我的图像任务的标签。

# Setup train and testing paths
train_dir = "code/Datasets/train/**/*.jpg"
test_dir = "code/Datasets/test/**/*.jpg"

train_dir, test_dir

check = glob.glob(train_dir)
print(check)

I also tried pasting some images to enable me try this and it did not work我还尝试粘贴一些图像以使我尝试此操作,但没有成功

train_dir = "code/Datasets/train/*.jpg"

Two possible issues:两个可能的问题:

  1. When you use ** in your glob path, you fail to pass recursive=True , so it won't actually treat ** as special当您在glob路径中使用**时,您无法通过recursive=True ,因此它实际上不会将**视为特殊
  2. Add import os , print(os.getcwd()) .添加import osprint(os.getcwd()) Is your working directory the one that contains code/Datasets/... ?您的工作目录是否包含code/Datasets/... Relative paths like this require a specific working directory (separate from the script directory).像这样的相对路径需要一个特定的工作目录(与脚本目录分开)。 If you were expecting it to use the script directory, you need to specify your path relative to __file__ , eg train_dir = os.path.join(__file__, "code/Datasets/train/**/*.jpg")如果您希望它使用脚本目录,则需要指定相对于__file__的路径,例如train_dir = os.path.join(__file__, "code/Datasets/train/**/*.jpg")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM