Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
davfra
Regular Visitor

Data retrieval with a Python script using dynamically generated paths.

Hello, everybody,


I have a problem accessing data with a Python script. The script does not find my dynamically generated path.

I want to read data from a JSON file located in the same folder as my Python script and my pbix file.

Because I don't know the parent folder structure, I want to dynamically create the absolute path to my JSON file in the Python script.

I use the following commands to do this:

#get absolute path from filename
from pathlib import Path
my_path = Path('myfile.json').absolute()

#Open the JSON file
        with open(my_path, encoding='utf-8') as file:
            json_data = json.load(file)
        return json_data['items']

However, I get the following error message during data retrieval.

Details: "ADO.NET: Þγţнόņ šċŗΐþŧ έґяθŗ.
Traceback (most recent call last):
  File "PythonScriptWrapper.PY", line 207, in <modules>
    etl.main()
  File "PythonScriptWrapper.PY", line 42, in main
    json_data = self.read_json_file()
  File "PythonScriptWrapper.PY", line 61, in read_json_file
    with open(str(self.source_path), encoding='utf-8') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Frank\\\PythonScriptWrapper_2c44fb75-27ca-4cf5-bfba-3a5035f935de\myfile.json

Strange is now: If I save the identical path as string in a variable to test it, the data retrieval works (my_path= 'C:\Users\\Frank\\\Desktop\\myfile.json').

However, as soon as I use the dynamically generated path, the data retrieval fails, even though the paths are completely identical.

Does anyone have a solution to my problem?

Thank you very much for your help.

Regards,

Frank

1 ACCEPTED SOLUTION
v-chuncz-msft
Community Support
Community Support

@davfra,

 

This seems to be a limitation.

https://docs.microsoft.com/en-us/power-bi/desktop-python-scripts#prepare-a-python-script

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-chuncz-msft
Community Support
Community Support

@davfra,

 

This seems to be a limitation.

https://docs.microsoft.com/en-us/power-bi/desktop-python-scripts#prepare-a-python-script

Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.