1

I get the wrong path back. The Datafile is in D:... and get everytime the path C:\Python27\lib\site-packages\xy back from python. I use the function

path = getcwd()

How can I fix it?

rtur
  • 133
  • 8
A.Boss
  • 207
  • 4
  • 8
  • Use `setcwd` to set the working directory to whatever you want it to be. – Tom Dalton Dec 02 '15 at 11:53
  • Okay the would be an option, but i don´t want to set. I want to read the path, because i won´t be change it in all Datafiles again. – A.Boss Dec 02 '15 at 11:57
  • 1
    getcwd gets the current path that your process is operating in - like when you cd into a directory in a terminal. If you tells us more about why you want to do this, we can probably be more help. E.g. what do you need the path for? – Tom Dalton Dec 02 '15 at 11:59
  • 1
    Where are you executing your script? If you are executing yor script in D:\... then the `getcwd` function will return that path. You're either executing the script from another location or executing a module in your C:\Python27\... directory which is calling `getcwd`. – Marco Bonelli Dec 02 '15 at 12:04
  • I need the current file to list file in the folder. I got a unknown number of files in the folder. My idea is, to put the python file in the folder in list me up for the following functions of the script. – A.Boss Dec 02 '15 at 12:04
  • I did´nt know my mistake, now it is working. Thanks – A.Boss Dec 02 '15 at 12:16

1 Answers1

1

You may be executing the script in a different place than your intended directory.

Solution 1: Move the .py file to the target directory, and execute it there.

  • Pros:
    • Easy
    • Works cross-platform (and for other users - if you do this, use getcwdu for Unicode)
    • No hard-coded path strings
  • Cons:
    • File must be in the same or higher directory as target folder

Solution 2: Manually write the string of the path to the folder.

  • Pros:
    • 'Just Works'
  • Cons:
    • Annoying bugs w/typos
    • Need to re-code every time you change directories
    • Won't work anywhere else
chaos
  • 32
  • 5