Try
Try

Reputation: 61

How to I not get double back slashes in my path?

I have this little part of a code that I am not understanding why I am getting double backslashes when I join the both paths together.

Here is the code

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

What am I missing? I keep getting this error

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'

Upvotes: 0

Views: 532

Answers (2)

Patrick Artner
Patrick Artner

Reputation: 51653

Differentiate between "display in your ide /debugging session" and "what is in the string". You might want to quit using python2 string formating and get raw-strings vs. normal strings straight:

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)

Output:

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) 

Output:

E:\Stock Database\Historical Data\Historical Stock List\stockfactsheet.csv
E:\Stock Database\Historical Data\Historical Stock List\stockfactsheet.csv

Doku:

Also: you can use / as well on windows as directory seperator - it works and it is less overall hassle if you need the strings for f.e. os.xxxx methods.

Upvotes: 0

Marcus
Marcus

Reputation: 3524

This is just the way that the python REPL prints out escaped characters. The actual strings only have a single backslash between each component of the path. You'll notice that the data printed from your print statement shows the single backslashes.

Upvotes: 1

Related Questions