0

I am using Selenium in Python to download the same file, but with different inputs, each time. So for example, I download data with country selection, "China." In the next iteration, I download the same data, but for country "Brazil."

I am struggling to find easy to understand syntax I can use to rename the downloaded files. The files are currently downloading as "Data.csv" and Data(1).csv." What I want is to have "China-Data.csv" and "Brazil-Data.csv."

The only relevant code I have constructed for this is:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

ChromeOptions=webdriver.ChromeOptions()
driver =webdriver.Chrome('Users/yu/Downloads/chromedriver')

inputcountry.send_keys('China')
inputcountry.send_keys(Keys.RETURN)

I read through this post, but I don't know how to create a forloop that can adapt this to the issue of files having the same name but with numbers at the end. EX: Data(1).csv, Data(2).csv, Data(3).csv

Thanks

Yus Ra
  • 97
  • 1
  • 6

2 Answers2

1

If you know the order of your files (i.e. you know that Data(1) should be named China-Data, Data(2) should be named Brazil-Data, etc.), then you just need to use a list and rename all the files according to it.

import os 

directory = 'Users/yu/Downloads/chromedriver/'
correct_names = ['China-Data.csv','Brazil-Data.csv']

def rename_files(directory: str, correct_names: list) -> None: 
    # change the name of each file in the directory
    for i, filename in enumerate(sorted(os.listdir(directory))): 
        src = directory + filename 
        dst = directory + correct_names[i]
        os.rename(src, dst) 

Every time you do inputcountry.send_keys('China'), you can add to the correct_names list whatever input you are giving, like correct_names.append('China-Data.csv').

You may call rename_files at the end with the correct_names list.

oamandawi
  • 405
  • 5
  • 15
1

Since you know the name of the download file, you can rename as you go. It can be tricky to know when a download completes, so I used a polling method.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
import shutil

download_file = os.path.expanduser("~/Downloads/Data.csv")
save_to_template = os.path.expanduser("~/Documents/Data-{}.csv")

# remove stale files
if os.path.isfile(download_file):
    os.remove(download_file)

ChromeOptions=webdriver.ChromeOptions()
driver =webdriver.Chrome('Users/yu/Downloads/chromedriver')

countries = ['China', 'Malaysia', 'Brazil']

for country in countries:
    inputcountry.send_keys(country)
    inputcountry.send_keys(Keys.RETURN)

    # one option is to poll for file showing up.... assuming file
    # is renamed when done
    for s in range(60): # give it a minute
        if os.path.exists(download_file):
            shutil.move(download_file, save_to_template.format(country))
            break
    else:
        raise TimeoutError("could not download {}".format(country))
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • This intuitively makes a lot of sense. But the code didn't work until I added a colon after ```if os.path.exists(download_file)```, for anyone else reading this – Yus Ra Feb 27 '20 at 20:51
  • Also should that be ```shutil.move``` instead of ```os.move```? Receiving error ```Attribute Error: module 'os' does not have 'move'``` – Yus Ra Feb 27 '20 at 22:53
  • @YuNa - Right! Fixed. – tdelaney Feb 27 '20 at 23:06