0

I've written a script in python in combination with selenium to download a file from a webpage by initiating a click on that file's link. When i run my script, the file seems to get downloaded in the predefined folder.

The problem is that I can't find any idea to rename the downloaded file. FYC there may be multiple files in that folder. I would like to rename the downloaded file to the variable newname in the script.

How can I rename a downloaded file from a folder?

This is I've written so far:

import os
from selenium import webdriver

url = "https://www.online-convert.com/file-format/docx"

folder_location = r"C:\Users\WCS\Desktop\file_storage"

newname = "document.docx"

def download_n_rename_file(link):
    driver.get(link)
    driver.find_element_by_css_selector("a[href$='example_multipage.docx']").click()
    #how to rename the downloaded file to "document.docx"
    #os.rename()

if __name__ == '__main__':
    chromeOptions = webdriver.ChromeOptions()
    prefs = {'download.default_directory': folder_location}
    chromeOptions.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(chrome_options=chromeOptions)
    download_n_rename_file(url)
robots.txt
  • 96
  • 2
  • 10
  • 36
  • 1
    `os.rename('existing_name.txt',newname)` – DirtyBit Feb 04 '19 at 09:19
  • Thanks for your suggestion @user5173426. I actually know how to use `os.rename()` but the problem is there is no such existing name in advance. I need to rename the file once it is available in the folder. Is there anyway I can determine in advance which name the file will get once it is downloaded? – robots.txt Feb 04 '19 at 09:27
  • Im afraid it won't be possible, unless you tweek around probably to loop through the downloaded folder inorder to check if the file with the extension `.crdownload` is completed and it no longer has this extension. you can then get its name and pass it to the `os.rename()` method. PS. the `.crdownload` way works with the Chrome. – DirtyBit Feb 04 '19 at 09:50

1 Answers1

0

I am assuming the file you've downloaded is named under example_multipage.docx:

import os
from selenium import webdriver

url = "https://www.online-convert.com/file-format/docx"

folder_location = r"C:\Users\WCS\Desktop\file_storage"

newname = "document.docx"

def download_n_rename_file(link):
    driver.get(link)
    driver.find_element_by_css_selector("a[href$='example_multipage.docx']").click()

    # To rename the downloaded file to "document.docx"
    os.rename('example_multipage.docx',newname)

if __name__ == '__main__':
    chromeOptions = webdriver.ChromeOptions()
    prefs = {'download.default_directory': folder_location}
    chromeOptions.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(chrome_options=chromeOptions)
    download_n_rename_file(url)

EDIT:

OP: but the problem is there is no such existing name in advance.

This makes me think, what if we could find when a file has downloaded successfully and then grab its name? But, wait. that is not possible!

Or could there be a way to detect the name of a downloaded file? But, wait. You don't have control over the download file naming through selenium.

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • 1
    It may take some time for the download to complete, while the `rename()` will be ran immediately after the click. – Todor Minakov Feb 04 '19 at 09:29
  • @TodorMinakov I guessed it, we could probably add a timer in between but that isn't the case if the name isn't known before. either way, I don't think it is possible. – DirtyBit Feb 04 '19 at 09:33
  • You are renaming a file with **current** name "X" (there's no way to rename a file if you don't know its current name, right? ;). As you are already making one assumption, just go with it - wait until that file X is actually present on the FS, this will tell you the download has finished. – Todor Minakov Feb 04 '19 at 09:56