1

This time I need help with python and paths manipulations. In first place i will will show you the structure im using on this set of apps:

  • MainFolder:
    • Folder1.
      • Subfolder1.
      • Subfolder2.
    • Folder2.

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 lunch a shell trought an Excel Macros, the path breaks. From time to time the code brakes(often in loops that use paths).

I need to lunch said scripts trought Excel or at least CMD. Because this will be for People who knows just enough about computers to make it every day. And it need to operate on everyone 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 writter(Wich 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 Lanaguage 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, cus' i'm launching the scripts trough a Shell from a excel macross button). Also i've updated the code on the class i've presented above, for stability in my applications.

dmb
  • 13
  • 4
  • Can you add a part of the code in your question? You can try playing with something like os.path.dirname(os.path.abspath(___file___) inside your code, but I don't know if this is the case unless I see the code. (It's actually only two underscores before and after "file") – umbe1987 Mar 24 '16 at 15:48
  • @umbe1987 I've added an update. Please be gentle I've started coding like 6 months ago. – dmb Mar 24 '16 at 18:20

0 Answers0