我有一段代码的一部分,我不明白为什么当我将两条路径连接在一起时为什么会出现双反斜杠。
这是代码
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'
答案 0 :(得分:1)
这只是python REPL打印出转义字符的方式。实际的字符串在路径的每个部分之间只有一个反斜杠。您会注意到,打印语句中打印的数据显示了单个反斜杠。
答案 1 :(得分:0)
区分“在您的ide /调试会话中显示”和“字符串中的内容”。 您可能要退出使用python2字符串格式,并直接获取原始字符串与普通字符串:
if (useFloat == FALSE)
for (size_t i = 0; i < 360; i++)
{
pRenderTarget->DrawLine(
D2D1::Point2F(
ellipse.radiusX * cosf(i * (float)M_PI / 180.0f) + ellipse.point.x,
ellipse.radiusY * -sinf(i * (float)M_PI / 180.0f) + ellipse.point.y),
D2D1::Point2F(
ellipse.radiusX * cosf(i * (float)M_PI / 180.0f * (multiplier / 100.0f)) + ellipse.point.x,
ellipse.radiusY * -sinf(i * (float)M_PI / 180.0f * (multiplier / 100.0f)) + ellipse.point.y),
pBrushLine);
}
else
for (size_t i = 0; i < 360; i++)
{
pRenderTarget->DrawLine(
D2D1::Point2F(
ellipse.radiusX * cosf(i * (float)M_PI / 180.0f) + ellipse.point.x,
ellipse.radiusY * -sinf(i * (float)M_PI / 180.0f) + ellipse.point.y),
D2D1::Point2F(
ellipse.radiusX * cosf(i * (float)M_PI / 180.0f * fMultiplier) + ellipse.point.x,
ellipse.radiusY * -sinf(i * (float)M_PI / 180.0f * fMultiplier) + ellipse.point.y),
pBrushLine);
}
输出:
database = "E:\\Stock Database\\Historical Data\\Historical Stock List\\"
# you need 1 space at the end, else \" is treated as escaped "
database_raw = r"E:\Stock Database\Historical Data\Historical Stock List\ ".-rstrip()
with_file = f"{database}\\stockfactsheet.csv"
with_file_raw = fr"{database}\stockfactsheet.csv" # no escapes needed
print(with_file)
print(with_file_raw)
输出:
database = "E:\\Stock Database\\Historical Data\\Historical Stock List\\"
# you need 1 space at the end, else \" is treated as escaped " - rstrip() removes it again
database_raw = r"E:\Stock Database\Historical Data\Historical Stock List\ ".rstrip()
with_file = f"{database}stockfactsheet.csv"
with_file_raw = fr"{database_raw}stockfactsheet.csv"
print(with_file)
print(with_file_raw)
Doku:
另外:您也可以在Windows上使用E:\Stock Database\Historical Data\Historical Stock List\stockfactsheet.csv
E:\Stock Database\Historical Data\Historical Stock List\stockfactsheet.csv
作为目录分隔符-它可以正常工作,并且如果需要字符串来减少麻烦,则总的来说麻烦较小。 /
种方法。