gpt4 book ai didi

python - 如何在我的路径中不出现双反斜杠?

转载 作者:行者123 更新时间:2023-12-01 08:28:23 25 4
gpt4 key购买 nike

我有一小部分代码,我不明白为什么当我将两条路径连接在一起时会得到双反斜杠。

这是代码

import time
import os
from selenium import webdriver

start = time.time()
sleep_time = 30
universe_data_site = 'http://www.csidata.com/?page_id=10'
database = "E:\\Stock Database\\Historical Data\\Historical Stock List\\"

chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory': database}
chrome_options.add_experimental_option(name='prefs', value= prefs)
stocks = webdriver.Chrome(r"E:\Python Programs\chromedriver", chrome_options = chrome_options)
#Website
stocks.get(universe_data_site)
#Navigate Web Page
stocks.find_element_by_css_selector('#ui-id-4').click()
stocks.find_element_by_css_selector('#stocks >a.blue_button.factbutton').click()
stocks.find_element_by_css_selector('body > a:nth-child(3)').click()
#Download and renaiming of File
filename = 'AllStocks.csv'
#removes existing file if already exists
if os.path.exists(r"%s%s"%(database,filename)) is True:
os.remove(r"%s%s"%(database,filename))
os.rename(r"%s"%database+"stockfactsheet.csv",r"%s%s"%(database,filename))
else:
os.rename(r"%s"%database+"stockfactsheet.csv",r"%s%s"%(database,filename))

time.sleep(sleep_time)
stocks.close()

我错过了什么?我不断收到此错误

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-24-194be27799ad> in <module>()
17 os.rename(r"%s"%database+"stockfactsheet.csv",r"%s%s"%(database,filename))
18 else:
---> 19 os.rename(r"%s"%database+"stockfactsheet.csv",r"%s%s"%(database,filename))
20
21 time.sleep(sleep_time)

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'E:\\Stock Database\\Historical Data\\Historical Stock List\\stockfactsheet.csv' -> 'E:\\Stock Database\\Historical Data\\Historical Stock List\\AllStocks.csv'

最佳答案

这就是 python REPL 打印转义字符的方式。实际的字符串在路径的每个组成部分之间只有一个反斜杠。您会注意到从打印语句打印的数据显示单个反斜杠。

关于python - 如何在我的路径中不出现双反斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54078312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com