Sphero
Sphero

Reputation: 333

Get current working directory with double backslashes instead of a single backslash in python

I am trying to obtain the current directory of the python script I am using and then use that path to add a filename (.exe and .xlsm files) to the path so that I can call them in other functions. This python script is being exported as an .exe file using pyinstaller and then called using VBA code from an excel macro. So far I have been obtaining the path as below:

import os
currPath = os.path.abspath(os.getcwd())

And this has been returning the following directory path:

C:\Users\USERNAME\Desktop

I would then open for example a .xlsm workbook like:

from openpyxl import load_workbook
wb = load_workbook (currPath + "\wbName.xlsm", data_only=True)

This executable works when I run it by itself but not when I run it from the Macro in VBA. After further testing and using hardcoded examples I have found that the filepath needs to be of the form:

C:\\Users\\USERNAME\\Desktop\\wbName.xlsm

So my question is how can I obtain the current directory with double backslashes instead of a single backslash so that I can then join the file names to the end of them. Thanks in advance.

Upvotes: 0

Views: 1259

Answers (1)

ElapsedSoul
ElapsedSoul

Reputation: 825

Try this:

currPath = currPath.replace('\\','\\\\')

Upvotes: 1

Related Questions