0

I have a program which makes use of the current working directory.

The path of the file that im running is: /home/pi/Test/file.py

When running os.getcwd() the path returned is /home/pi/

the path i want is /home/pi/Test

what am i missing?

    osDir = os.getcwd()

month = osDir + "/" + month

print (osDir)
print (month)


if not os.path.exists(month):
        os.makedirs(month)

Using Python3 .

The command to run the file is pi@raspberrypi:~ $ python3 TEST/file.py

Morrowj04
  • 9
  • 3

2 Answers2

2

os.getcwd() does not return the path to the script; it returns the path to the folder where the script was started from, in your case /home/pi. If you want to get the absolute path to the python script, use os.path.abspath(os.path.realpath(__file__)) instead.

And instead of osDir + "/" + month you should use os.path.join(osDir, month).

TheEagle
  • 5,808
  • 3
  • 11
  • 39
0

Relative path of the file print(os.path.realpath(__file__))

for pathname use print(os.path.abspath(os.curdir))

Ali Aref
  • 1,893
  • 12
  • 31