排除特定文件的模式

发布于 2025-01-23 05:06:56 字数 1159 浏览 1 评论 0原文

我正在尝试创建正则表达式,该正则将在某些目录中列出所有.jpg,但少数文件(静态,而不是模式)。

因此,我已经写了这篇文章:(Python)

"^(?!358097_sat!823133_sat!140860_sat).*jpg$"
"^(?!358097_sat|823133_sat|140860_sat).*jpg$"

我想列出所有JPEG文件,除了:

  • 358097_SAT
  • 823133_SAT
  • 140860_SAT

,但它给我一个错误,说没有找到与此模式相匹配的文件。

这是完整的。字符串和错误消息:

No files matched pattern: ../input/dataset/valid/^(?!358097_sat!823133_sat!140860_sat).*jpg$

我实际上将此正则态度传递给TF功能:

tf.data.Dataset.list_files(dataset_path + val_data + "^(?!358097_sat|823133_sat|140860_sat).*jpg$", seed=SEED)
# dataset_path = "../input/dataset/"
# val_data = "valid/"

完全错误:

*InvalidArgumentError:预期'tf.tensor(false,shape =(),dtype = bool)'为true。汇总数据:b'no文件匹配模式:../ input/dataset/valive/^(?!358097_SAT | 823133_SAT | 140860_SAT)。 jpg $'

这是功能参考:< “ https://www.tensorflow.org/api_docs/python/tf/data/dataset#list_files” rel =“ nofollow noreferrer”> https:///www.tensorflow.org.org/papi_org/api_docs/api_docs/pytyon/python/python/tff/datatap /ddatapat, list_files

I'm trying to create regex which will list all .jpg in some directory except few files(static, not a pattern).

So, I've wrote this:(Python)

"^(?!358097_sat!823133_sat!140860_sat).*jpg
quot;
"^(?!358097_sat|823133_sat|140860_sat).*jpg
quot;

I want to list all JPEG files except for:

  • 358097_sat
  • 823133_sat
  • 140860_sat

But it gives me an error saying that no file found matching this pattern.

Here is the complete string and error message:

No files matched pattern: ../input/dataset/valid/^(?!358097_sat!823133_sat!140860_sat).*jpg$

I'm actually passing this regex to a tf-function:

tf.data.Dataset.list_files(dataset_path + val_data + "^(?!358097_sat|823133_sat|140860_sat).*jpg
quot;, seed=SEED)
# dataset_path = "../input/dataset/"
# val_data = "valid/"

Complete error:

*InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true. Summarized data: b'No files matched pattern: ../input/dataset/valid/^(?!358097_sat|823133_sat|140860_sat).jpg$'

Here is the function reference: https://www.tensorflow.org/api_docs/python/tf/data/Dataset#list_files

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

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

发布评论

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

评论(1

寒尘 2025-01-30 05:06:56

静态方法list_files期望包含不是正则表达式。
另请参见
filename匹配

使用 globs 匹配的文件名匹配没有方法来否定匹配。因此,您将必须编写自定义功能才能做到这一点。

您可以使用eg glob.glob()生成JPEG文件列表,然后过滤符合符合您的字符串的文件。

ignore = ("358097_sat", "823133_sat", "140860_sat")

files = [f for f in glob.glob("*.jpg") if not any(j in f for j in ignore)]

The static method list_files expects a string or list of strings containing globs, not regular expressions.
See also filename matching.

Filename matching using globs does not have a way to negate a match. So you will have to write a custom function to do that.

You could use e.g. glob.glob() to generate a list of JPEG files, and then filter out the ones that match your strings.

ignore = ("358097_sat", "823133_sat", "140860_sat")

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