I have a Selenium(Chrome) script that goes to a URL and downloads a bunch of files sequentially. But the names are all gibberish and as such neither Selenium nor Chrome has control over the name of the downloaded file. So what I want to do is watch the download directory for any new files that are created and then rename them on creation with a name of my own choosing.
How do I go about this? I've heard that watchdog
is a good package to create and log an EventListener. But then how do I dynamically pass the handler a particular name when the on_created
event is triggered? Is watchdog the right way to go or is there some other solution that can work?
NOTE: I did try grabbing all the in the directory using glob
and then update the name of the latest file by comparing the creation time but that results in a logical error as it mixes up the filenames in case the new file hasn't downloaded by the time this method is executed. I've attached the code below for this method.
def __rename_downloaded_file(self, filename: str):
"""Rename the latest download file to the given name"""
# TODO create a listener instead of the while loop
while True:
# keep looping in case there are no file in directory.
list_of_files = glob.glob(f"{self.download_path}\\*.pdf")
if len(list_of_files) > 0:
break
latest_file = max(list_of_files, key=os.path.getctime)
print(latest_file)
head, _ = os.path.split(latest_file)
new_filename = os.path.join(head, filename+'.pdf')
print(new_filename)
os.rename(latest_file, new_filename)
The answer is here.
@Raghavendra Phayde thank you for putting me on this thread.
The list of downloads you get is sorted by latest download timestamp. You can then rename each file with the below code.
for old_filename, new_filename in zip(downloaded_file_names, rename_list):
head, _ = os.path.split(old_filename)
new_file_name_path = os.path.join(head, new_filename + '.pdf')
# print(old_filename)
# print(new_file_name_path)
# print('\n')
os.rename(old_filename, new_file_name_path)