如何不使用glob显示目录中的特定值?

问题描述 投票:0回答:1

我在这里有此代码:

    # Create a Tkinter variable
    self.tkvar = StringVar()

    # Directory
    self.directory = "C:/Users/label/Desktop/Sign off project/test only/to sign"
    self.choices = glob.glob(os.path.join(self.directory, "*")) #all choices

这为我提供了特定文件夹中的所有文件名。如何添加条件以不返回以"test.docx"

结尾的值

不确定这是否有帮助:

self.popupMenu = OptionMenu(main, self.tkvar, *self.choices) # Dropdown menu of all files inside the folder
python
1个回答
0
投票

您可以尝试使用列表理解:

# Create a Tkinter variable
self.tkvar = StringVar()

# Directory
self.directory = "C:/Users/label/Desktop/Sign off project/test only/to sign"
self.choices = glob.glob(os.path.join(self.directory, "*")) #all choices
# list comprehension to filter 
self.choices = [f for f in self.choices if 'test.docx' not in f]

这将从self.choices中删除所有包含'test.docx'的元素]

© www.soinside.com 2019 - 2024. All rights reserved.