0

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!

1 Answers1

0

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.

  • hello there, that was such a great help, which it solved my previous issues when reading files dynamically and then extract it to another folder path. however, i dont know why there is an error showing "[Errno 2] No such file or directory: 'Filename.zip' when it can actually find the file there. can you please help me once again if you have time to do so? thanks! – Jimmylee4real Mar 08 '21 at 02:58
  • Not sure if this is your problem, but to access the file, you need to specify it's full path. This can either be the absolute path of the file in your system, or a relative path starting from the location where you run your python program. For example, if the location you run your code from has a subfolder named Downloads in which Filename.zip is located, you need to access it by "Downloads/Filename.zip" or with the os module using os.path.join("Downloads","Filename.zip"). – kappablanca Mar 08 '21 at 07:07
  • thanks, it solved my problem! one more question please, is it possible to unzip the file and look for specific files? let's say I only want to extract file starting with "ABC.csv". is it possible? do you have any sample code? thanks!!! – Jimmylee4real Mar 09 '21 at 13:18