0

I have the following python script (script.py):

#!/usr/bin/env python3
import os

os.makedirs('./downloads/')

Which simply creates a directory named 'downloads' in the directory, where the script.py file is located.

Now I want this program to be run as a cronjob in linux. So the command for that is:

./home/pi/application/script.py

The folder, the program creates should be created under '/home/pi/application/' but it is created in the root directory '/'

How can I fix this?

Michel1506
  • 73
  • 1
  • 7
  • Possible duplicate of [How to get full path of current file's directory in Python?](https://stackoverflow.com/questions/3430372/how-to-get-full-path-of-current-files-directory-in-python) – BiBi Jul 15 '18 at 12:03

1 Answers1

0

To get the path where script.py is rather than the path from where you call the script (working directory) you can use:

os.path.dirname(os.path.abspath(__file__))

In you example, the previous command would return /home/pi/application/ (if that is the full path).

BiBi
  • 7,418
  • 5
  • 43
  • 69