我正在尝试使用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)
推荐答案就我而言,我忘记了转义使用 glob.escape(pathname)转义目录名称中的特殊字符 [.
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始终返回空列表
发布评论