0

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'
Try
  • 61
  • 1
  • 9

2 Answers2

1

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.

Marcus
  • 3,216
  • 2
  • 22
  • 22
0

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.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • I modified my code above. The issue arises for the #removes existing file if already exists portion. What I'm trying to get is 1- If the csv download does not exist in the path folder create csv file as "Allstocks.csv" 2- if the file path folder has the file name "Allstocks.csv" delete that csv file and the new downloaded csv file to be named "Allstocks.csv" – Try Jan 07 '19 at 17:25