Python glob.glob始终返回空列表

编程入门 行业动态 更新时间:2024-10-25 04:25:22
本文介绍了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)

推荐答案

就我而言,我忘记了转义使用 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始终返回空列表

本文发布于:2023-10-07 14:38:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469655.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   Python   glob

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!