0

My Goal:

Current goal for me is to return values that don't contain the value test.docx

My Current code:

I have this code here:

    # 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

What it does:

That gives me all file names inside a specific folder. How can I add a condition not to return me values that end with "test.docx"

Would your answer depend on the following code?

I have this code here, it's a OptionMenu / drop down menu of values in tkinter.

I have added this in, as I believe the code may be slightly different when working with drop down menu's

self.popupMenu = OptionMenu(main, self.tkvar, *self.choices) # Dropdown menu of all files inside the folder

2 Answers 2

1

You could try using list comprehension:

# 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]

This would remove any elements from self.choices that contain 'test.docx'

3
  • I like the fact that it is "containing" rather than "ending" with! Do you know any research material, where I can properly study and understand this? Thanks Commented Oct 16, 2019 at 10:26
  • Hi, just realized... I am unpacking self.choices, I require it to be a listlike Commented Oct 16, 2019 at 16:54
  • What about files like "c:\test.docx.bak"?
    – CristiFati
    Commented Nov 1, 2019 at 15:57
1

you can always filter the results of glob:

    # 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
    self.choices = list(filter(lambda x: not x.endswith("test.docx"), self.choices))

the filter returns a generator, so you might want to convert it to a list, depending on what you're doing with it.

edit: I saw that you are unpacking self.choices, this does require it to be a listlike

4
  • Hi, instead if ending with, is it possible to show containing? Commented Oct 16, 2019 at 17:04
  • What about files like "c:\dummy_test.docx"?
    – CristiFati
    Commented Nov 1, 2019 at 15:57
  • @98Ed yes, replace not x.endswith("test.docx") with "test.docx" not in x Commented Nov 5, 2019 at 19:59
  • @CristiFati this solution will filter out files which end with the string "test.docx", like the name you mentioned. You may need to change the directory variable if the file is in the root "c:\\" directory. Commented Nov 5, 2019 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.