0

I want to pause my python script while waiting for a file download to happen. I don't want to use an explicit wait. I want this to run fast and not rely on an explicit wait. I'm kinda noobish but here is what I have tried.

file_check = glob.glob1('downloads', '*.pdf')
        while not os.path.isfile(str(file_check)):
            time.sleep(1)

I used the str() because it complained about needing a string for the path. I have a feeling this isn't the way to properly do this. so how should I dynamically wait for a file download?

P.S My .pdf file downloads into '/downloads', and my pdf is dynamically named before download so that's why I need globs wildcard.

GustavoIP
  • 873
  • 2
  • 8
  • 25
Gdfelt
  • 161
  • 15

1 Answers1

0

When you do file_check = glob.glob1('downloads', '*.pdf') the result of glob.glob1(...) is stored in file_check just once and that's it. In this case, if you enter inside the while loop you never will get out of there because file_check will not change (except if there are threads or stuff like that that can modify their value externally).

glob.glob and glob.glob1 (this one it's not even public, as you can see the docs) returns a list. If 'downloads' folder it's empty, you will get an empty list []. In Python, lists have an implicit booleanness, so if the list it's empty, in a conditional statement it will be interpreted as False, or 'True' if it's not empty.

Rewriting your code the result will be something like this:

while not glob.glob1('downloads', '*.pdf'):
    time.sleep(1)
Community
  • 1
  • 1
GustavoIP
  • 873
  • 2
  • 8
  • 25