Python glob.glob始终返回空列表 [英] Python glob.glob always returns empty list

查看:219
本文介绍了Python glob.glob始终返回空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用glob和os在目录中找到最新的.zip文件.有趣的是,我进行了以下设置,并且以前可以正常运行:

I'm trying to use glob and os to locate the most recent .zip file in a directory. Funny thing is, I had the following set up and it was working previously:

max(glob.glob('../directory/*.zip'), key=os.path.getctime)

现在运行此命令使我得到max()arg是一个空序列,这很有意义,因为当我尝试这样做时:

Running this now gets me max() arg is an empty sequence, which makes sense because when I try this:

glob.glob('../directory/*.zip')

它只返回一个空列表.使用完整路径也会给我一个空列表.尝试其他目录也可以得到一个空列表.考虑到以前的工作原理,我对这里发生的事情感到非常困惑.帮助吗?

it returns nothing but an empty list. Using the full path also gets me an empty list. Trying other directories also gets me an empty list. I'm very confused about what's going on here given this worked perfectly previously. Help?

使用以下命令再次使它工作: glob.glob(/Users/*/directory/*.zip)

Got it to work again using: glob.glob(/Users/*/directory/*.zip)

推荐答案

就我而言,我忘记了

In my case, I forgot to escape the special character [ that was in the directory name using glob.escape(pathname).

因此,请尝试使用 glob.glob(glob.escape(pathname)).而不是 glob.glob(pathname).

So instead of glob.glob(pathname), try glob.glob(glob.escape(pathname)).

这篇关于Python glob.glob始终返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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