Search code examples
pythondatabricksazure-databricks

How to Read Python .py file code from azure databricks workspace


How to Read Python .py file code from azure data bricks workspace? I am trying to encrypt and decrypt the python files using fernet library I have the source code in data bricks workspace, I wanted to read that source code in python, I used below code:

with open('/Users/abc@gmail.com/Encrypt_Decrypt_POS/Sample.ipynb','r') as f_read:
file_data = f_read.read()

But I am getting below error:

FileNotFoundError: [Errno 2] No such file or directory: '/Users/abc@gmail.com/Encrypt_Decrypt_POS/Sample

Solution

  • To access files in the workspace, you need to prefix the path with /Workspace and you need to have a specific DBR version (see docs) - you can just click right on the file in the new UI and select (Copy Path). But it's not applicable to the notebooks as it's explained in this answer, together with workaround.

    with open('/Workspace/Users/abc@gmail.com/Encrypt_Decrypt_POS/Sample.ipynb','r') as f_read:
    file_data = f_read.read()
    

    But it looks like that you want to include the code for reuse in another notebook - in this case you can just include notebooks content using the %run command, like this (you don't need to specify a file extension for notebooks):

    %run /Users/abc@gmail.com/Encrypt_Decrypt_POS/Sample