Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当文件具有相同名称时,如何在for循环中重命名下载的文件?_Python_Selenium - Fatal编程技术网

Python 当文件具有相同名称时,如何在for循环中重命名下载的文件?

Python 当文件具有相同名称时,如何在for循环中重命名下载的文件?,python,selenium,Python,Selenium,我在Python中使用Selenium下载相同的文件,但每次都有不同的输入。例如,我下载了国家选择“中国”的数据。在下一次迭代中,我下载了相同的数据,但国家选择“巴西” 我正在努力找到易于理解的语法,我可以用它来重命名下载的文件。这些文件目前正在下载为“Data.csv”和“Data(1.csv)”,我想要的是“China Data.csv”和“Brazil Data.csv” 我为此编制的唯一相关代码是: from selenium import webdriver from selenium

我在Python中使用Selenium下载相同的文件,但每次都有不同的输入。例如,我下载了国家选择“中国”的数据。在下一次迭代中,我下载了相同的数据,但国家选择“巴西”

我正在努力找到易于理解的语法,我可以用它来重命名下载的文件。这些文件目前正在下载为“Data.csv”和“Data(1.csv)”,我想要的是“China Data.csv”和“Brazil Data.csv”

我为此编制的唯一相关代码是:

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)
我通读了一遍,但我不知道如何创建一个forloop来适应文件同名但结尾有数字的问题。例如:Data(1).csv,Data(2).csv,Data(3).csv


谢谢

如果您知道文件的顺序(即,您知道数据(1)应命名为中国数据,数据(2)应命名为巴西数据等),那么您只需使用列表并根据列表重命名所有文件即可

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) 
每次您输入国家/地区。发送密钥(“中国”)时,您可以将您输入的任何内容添加到正确的名称列表中,如
正确的名称。追加('China-Data.csv')


您可以使用正确的名称列表在末尾调用重命名文件。

如果您知道文件的顺序(即,您知道数据(1)应命名为中国数据,数据(2)应命名为巴西数据等),则只需使用列表并根据列表重命名所有文件即可

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) 
每次您输入国家/地区。发送密钥(“中国”)时,您可以将您输入的任何内容添加到正确的名称列表中,如
正确的名称。追加('China-Data.csv')


您可以在末尾使用正确的\u名称列表调用rename\u files。

因为您知道下载文件的名称,所以可以在运行时进行重命名。要知道下载何时完成可能很困难,因此我使用了轮询方法

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))

由于您知道下载文件的名称,因此可以随时重命名。要知道下载何时完成可能会很困难,因此我使用了轮询方法

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))

这从直觉上讲是很有道理的。但是,如果os.path.exists(下载_文件),直到我在
之后添加了冒号,代码才起作用
,对于其他阅读本文的人来说,也应该是
shutil.move
而不是
os.move
?接收错误
属性错误:模块“os”没有“move”
@YuNa-对!已修复。这从直觉上讲是很有意义的。但是,如果os.path.exists(下载文件)存在,则在
之后添加冒号,代码才起作用
,对于阅读本文的任何其他人,也应该是
shutil.move
而不是
os.move
?接收错误
属性错误:模块“os”没有“move”
@YuNa-对!已修复。