0

My existing python script is in the One Drive and I want to get its path. I have tried all the solutions but none of them worked.

This code did not work at all.

import os
sys.getcwd()

It brought C"\users\hkay. My python file is in the one drive. The closest solution was this one.

from inspect import getsourcefile
from os.path import abspath

abspath(getsourcefile(lambda:0)) 

This solution seems to bring the downloaded version of OneDrive in the temporary folder as shown below but the file is actually in OneDrive in C:\Users\hkay\OneDrive\Desktop\Project A\

'D:\TEMP\hkay\Temp\ipykernel_179044\1451747622.py'

hkay
  • 159
  • 1
  • 2
  • 12

1 Answers1

0

Have a look at this question on stackoverflow link.
It explains what __file__ means in python.
To sum it up, it returns the path where the script was called and in combination with the os module, I think you can get the script directory even from One Drive. Hope I helped you.

Liuk23
  • 58
  • 7