繁体   English   中英

如果包含某个单词,如何列出文件列表,但如果包含其他单词,则使用 glob python 排除?

[英]How to list down the list of file if contains certain word but exclude if contain other word using glob python?

我想列出包含某些单词的文件,但如果它包含另一个特定名称,则需要将其排除

我的文件如下:

C:\\Users\\Users\\Desktop\\Data\\Batch_123.xlsx    ---- file 1
C:\\Users\\Users\\Desktop\\Data\\Batch_456.xlsx    ---- file 2
C:\\Users\\Users\\Desktop\\Data\\Batch_Red_1.xlsx  ---- file 3
C:\\Users\\Users\\Desktop\\Data\\Batch_Red_2.xlsx  ---- file 4
C:\\Users\\Users\\Desktop\\Data\\Batch_Yellow_1.xlsx ---- file 5
C:\\Users\\Users\\Desktop\\Data\\Batch_Yellow_2.xlsx  ---- file 6

我想要文件名: Batch_123.xlsx and Batch_456.xlsx只排除那些带有*_1.xlsx, *_2.xlsx, *_3.xlsx, *_4.xlsx, and so on

我试过使用: glob.glob(path + "**/Batch_*[!_1].xlsx", recursive=True)但它只返回到文件 1,2,4,6

有没有办法使这成为可能?

这种方法怎么样?

set(glob.glob(path+"**/Batch_*.xlsx", recursive=True)) - set(glob.glob(path+"**/Batch_*_*.xlsx", recursive=True))

我知道可能有更好的方法,但这对初学者来说很简单明了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM