Reputation: 13
This time I need help with python and paths manipulations. In first place I will will show you the structure I'm using on this set of apps:
The folder I have the scripts in, is Folder 2. But I need the path to Main Folder(1 back from Folder2). But the method I'm using, isn't quite reliable.
Currently I'm using os.getcwd()
, but if I launch a shell through Excel Macros, the path breaks. From time to time the code breaks(often in loops that use paths).
I need to launch said scripts through Excel or at least CMD. Because this will be for People who knows just enough about computers to make it every day. And it needs to operate on everyone's machines.
PS: The scripts works just fine, but they do need to be in a separate folder from the files they are working on.
UPDATE 1 AS REQUESTED:
I've made a class and this is the class
class mypath:
def Revpath(self):
CWD = os.getcwd()
Revpaths = CWD[:-14]
return Revpaths
def Saldos(self):
CWD = os.getcwd()
Revpaths = CWD[:-14]
Cuadraje = Revpaths+"Stock\\Saldos"
return Cuadraje
def Banco(self,IDkind):
CWD = os.getcwd()
Revpaths = CWD[:-14]
Stocks = Revpaths+"Stock\\kind\\"+IDkind
return Stocks
mp = mypath()
Then I just assign the returned value to a Var
. One is used on a CSV writer(Which happens to be the most common miss path). And the other to set a download folder, for a Firefox profile. Each script uses the same Class, and the logic is 100% the same on every script. But I only can show you this much, because the code on itself is messy and have sensitive data in it.
UPDATE 2: SOLVED
Solved this by replacing os.getcwd()
for os.path.realpath(__file__)
Because of language (First Language is Spanish), I've assumed that the Current Working Directory was the one with the script, instead it returned the PyCharm Settings folders(I'm still clueless about this, because I'm launching the scripts through a Shell from an Excel macros button).
Also I've updated the code on the class I've presented above, for stability in my applications.
Upvotes: 1
Views: 116