【问题标题】:Simple glob command not returning anything in print statement简单的 glob 命令在打印语句中不返回任何内容
【发布时间】:2021-08-11 13:17:22
【问题描述】:

我有这段代码可以返回我感兴趣的文件的路径列表。但是,当我在 putty 中运行这段代码时,没有返回任何打印语句;在命令行上没有任何反应。

我知道提供的路径应该返回一个输出,就像在命令行上成功使用它一样。

代码如下:

from glob import glob

#using glob package to find files for models

file_list = glob.glob('/soge-home/data/model/cmip6/CMIP6/CMIP/*/*/historical/r1i1p1f1/Amon/pr/gn/latest/*.nc', recursive=True)
file_list.sort()

#printing file names

for ifile in file_list:
    print(ifile)

【问题讨论】:

    标签: python glob


    【解决方案1】:

    您已经在导入 glob.glob。不要调用 glob.glob(),改用 glob()

    from glob import glob
    
    #using glob package to find files for models
    
    file_list = glob('/soge-home/data/model/cmip6/CMIP6/CMIP/*/*/historical/r1i1p1f1/Amon/pr/gn/latest/*.nc', recursive=True)
    file_list.sort()
    
    #printing file names
    
    for ifile in file_list:
        print(ifile)
    

    【讨论】:

    • 不清楚recursive=True 在这里如何有意义,除非每个*.nc 叶实际上是一个目录。 OP 更有可能想要 /soge-home/data/model/cmip6/CMIP6/CMIP/**/historical/r1i1p1f1/Amon/pr/gn/latest/*.nc 而不需要 recursive=True ,其中 ** 扩展为任意嵌套的目录层次结构,但实际上,他们应该澄清他们拥有什么以及他们想要什么。
    • 另外,glob 已经对结果进行了排序,所以真的不需要再单独排序了。
    猜你喜欢
    • 2017-12-28
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 2012-08-06
    • 2021-06-10
    相关资源
    最近更新 更多