cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the path of files using python os library

sp3234
New Contributor

unable to find the path to directory with os library

0693f000007OoJdAAK

7 REPLIES 7

User16752239222
New Contributor II
New Contributor II

Hi @sp3234​ 

When using the local file APIs to read and write to dbfs paths, you should reference using "/dbfs/..." rather than "dbfs/..."

Here's an example that compares the result of local file APIs to using the %fs magic keyword in a notebook cell.

screen-shot-2018-08-31-at-103435-am.png 

reference documentation

Wistech_biz
New Contributor II

Being a web developer I've used this:

>>> import os >>> os.path.abspath("C:/example/cwd/mydir/myfile.txt") 'C:/example/cwd/mydir/myfile.txt'

rickwilliam
New Contributor II

I am working with Magento 2 extensions development company and i am a python developer since 5 years here is my answer.

 Python program to explain os.getcwd() method 

# importing os module 
import
os 

# Get the current working 
# directory (CWD) 
cwd 
=
os.getcwd() 

# Print the current working 
# directory (CWD) 
print
(
"Current working directory:"
) 
print
(cwd) 

Hi,

os.getcwd() simply prints '/databricks/driver' for me irrespective of the location of the file..

Ideally I would like to get "/dbfs/FileStore/shared_uploads/krishna@company.com/Project_Folder"

Thanks

tomjenner
New Contributor II

In my opinion, it's hard to get the path of files using python os library. As a programmer who is engaged in software development for small business, I'm interested in the answer too.

Hi mate, did you get the answer?

os.getcwd() simply prints '/databricks/driver' for me irrespective of the location of the file..

Ideally I would like to get "/dbfs/FileStore/shared_uploads/krishna@company.com/Project_Folder"

Thanks

Aviral-Bhardwaj
Esteemed Contributor III

@sp3234​ 

Please use this it will work

image