Python4Capella Accessing Files Outside of Workspace

Hi everyone,

I’ve been working in Python4Capella (I’m very new, but doing my best), and I’m hoping to use it to read an excel file from a drive on my computer, which is not in the Capella workspace. I would rather not move the file into the Capella workspace, but I would like to access it to calculate percentage values based on PVMT information accessed in a P4C script.

I haven’t seen anything on the forum or in the docs about this. I’ve only seen some information about saving scripts outside of the workspace, which is not what I’m trying to do. Is it possible to read data files stored outside of the Capella workspace using Python4Capella?

Thank you,
Karley

Welcome Karley,

You can use an absolute file path on your system or a relative path to access the file. The current working directory may change depending on the interpreter you are using. So before using a relative path, you can check the present working directory like this:

import os 

dir_path = os.path.dirname(os.path.realpath(__file__))
print(dir_path)

Then if you are using openpyxl, you can open your Excel file like this:

wb = load_workbook(xlsx_file_path)

More details on how you can use the Excel file in this sample script for instance. The sample script get the file from the workspace then use its absolute path to open it.

1 Like