Search code examples
pythonseleniumzipunzip

how to extract a continuously updable Zip file on a website by Selenium and then unzip it for specific file names


I have used Selenium x Python to download a zip file daily but i am currently facing a few issues after downloading it on my local download folder

  1. is it possible to use Python to read those files dynamically? let's say the date is always different. Can we simply add wildcard*? I am trying to move it from downloader folder to another folder but it always require me to name the file entirely.

  2. how to unzip a file and look for specific files there? let's say those file will always start with files names "ABC202103xx.csv"

much appreciate for your help! any sample code will be truly appreciate!


Solution

  • Not knowing the excact name of a file in a local folder should usually not be a problem. You could just list all filenames in the local folder and then use a for loop to find the filename you need. For example let's assume that you have downloaded a zip file into a Downloads folder and you know it is named "file-X.zip" with X being any date.

    import os
    for filename in os.listdir("Downloads"):
        if filename.startswith("file-") and filename.endswith(".zip"):
            filename_you_are_looking_for = filename
            break
    

    To unzip files, I will refer you to this stackoverflow thread. Again, to look for specific files in there, you can use os.listdir.