全局字符和方括号字符 ('[]')

发布于 2024-08-28 12:56:18 字数 339 浏览 11 评论 0原文

/Users/smcho/Desktop/bracket/[10,20] 目录有“abc.txt”,但是当我运行此 Python 代码时

import glob
import os.path

path1 = "/Users/smcho/Desktop/bracket/\[10,20\]"
pathName = os.path.join(path1, "*.txt")
print glob.glob(pathName)

它返回一个空列表。

  • Python 的 glob 不能处理括号字母或其他字母吗?
  • 有什么办法可以解决这个问题吗?

/Users/smcho/Desktop/bracket/[10,20] directory has "abc.txt", but when I run this Python code

import glob
import os.path

path1 = "/Users/smcho/Desktop/bracket/\[10,20\]"
pathName = os.path.join(path1, "*.txt")
print glob.glob(pathName)

It returns an empty list.

  • Can't Python's glob handle the bracket letters or others?
  • Is there any way to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忘东忘西忘不掉你 2024-09-04 12:56:18

glob 中的括号用于字符类(例如,[az] 将匹配小写字母)。您可以将每个括号放在字符类中以强制它们匹配:

path1 = "/Users/smcho/Desktop/bracket/[[]10,20[]]"

[[] 是仅包含字符 [[]]< 的字符类/code> 是仅包含字符 ] 的字符类(右括号可以通过将其放在第一个位置来放置在字符类中)。

此外,由于括号在字符串文字中不会转义,因此您的代码将查找反斜杠和括号。

The brackets in glob are used for character classes (e.g. [a-z] will match lowercase letters). You can put each bracket in a character class to force them being matched:

path1 = "/Users/smcho/Desktop/bracket/[[]10,20[]]"

[[] is a character class containing only the character [, and []] is a character class containing only the character ] (the closing bracket can be placed in a character class by putting it in the first position).

Additionally, since brackets aren't escaped in string literals, your code will look for a backslash as well as a bracket.

奶茶白久 2024-09-04 12:56:18

在Python 3.4中,您可以使用 glob.escape< /a>.

In Python 3.4 you can use glob.escape.

梦萦几度 2024-09-04 12:56:18

glob 在底层使用 fnmatch。您可以直接使用它:

import fnmatch, os

names = os.listdir("/Users/smcho/Desktop/bracket/[10,20]")
print fnmatch.filter(names, '*.txt')

或者使用(非公共)glob.glob1()(它至少存在于Python 2.3+,包括Python 3中):

import glob

print glob.glob1("/Users/smcho/Desktop/bracket/[10,20]", '*.txt')

这是glob.glob1

def glob1(dirname, pattern):
    if not dirname:
        dirname = os.curdir
    if isinstance(pattern, unicode) and not isinstance(dirname, unicode):
        dirname = unicode(dirname, sys.getfilesystemencoding() or
                                   sys.getdefaultencoding())
    try:
        names = os.listdir(dirname)
    except os.error:
        return []
    if pattern[0] != '.':
        names = filter(lambda x: x[0] != '.', names)
    return fnmatch.filter(names, pattern)

glob uses fnmatch under the hood. You could use it directly:

import fnmatch, os

names = os.listdir("/Users/smcho/Desktop/bracket/[10,20]")
print fnmatch.filter(names, '*.txt')

Or using (non-public) glob.glob1() (it is present at least in Python 2.3+ including Python 3):

import glob

print glob.glob1("/Users/smcho/Desktop/bracket/[10,20]", '*.txt')

Here's the implementation of glob.glob1:

def glob1(dirname, pattern):
    if not dirname:
        dirname = os.curdir
    if isinstance(pattern, unicode) and not isinstance(dirname, unicode):
        dirname = unicode(dirname, sys.getfilesystemencoding() or
                                   sys.getdefaultencoding())
    try:
        names = os.listdir(dirname)
    except os.error:
        return []
    if pattern[0] != '.':
        names = filter(lambda x: x[0] != '.', names)
    return fnmatch.filter(names, pattern)
老旧海报 2024-09-04 12:56:18

您可以使用 path.replace('[', '[[]')glob 正确处理任意输入路径。

You could use path.replace('[', '[[]') to have arbitrary input paths handled by glob correctly.

野味少女 2024-09-04 12:56:18

glob.escape 将转义通配符。因此使用

glob.glob(f"{glob.escape(pathName)}*")

glob.escape will escape the wildcard. Therefore use

glob.glob(f"{glob.escape(pathName)}*")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文