zonfl
zonfl

Reputation: 328

Python won't accept file path when executed

Encountered a strange problem. The following code snippet won't run if I execute the "load_data.py" file. However, when I run the lines in an IDE console it runs without problems. Hard coding the file path would work, and I have read solutions that suggested appending the data path to PYTHONPATH, however I'm not sure this is a good solution because I want to push this to a Docker container.

Can someone help me figure out what seems to be the problem here?

import pandas as pd
from os import path

def _preprocess_data(data_path):

    try:
        ## load data ##
        data = pd.read_json(data_path)

    except ValueError:
         print("File not found. Check the path variable and filename")
         exit()

if __name__ == '__main__':
    print('Preprocessing data...')
    #### Preparation ####
    file_path = path.abspath('load_data.py')  # full path of your script
    dir_path = path.dirname(file_path)  # full path of the directory of your script
    json_file_path = path.join(dir_path, 'data/clean_data.json')  # absolute zip file path
    _preprocess_data(data_path=json_file_path)

Upvotes: 0

Views: 710

Answers (1)

Cole Mollica
Cole Mollica

Reputation: 28

It looks like the reason your code is not working is because you're executing the code from another directory where load_data.py is not, so the relative path doesn't exist. Try changing your code to

import pandas as pd
from os import path

def _preprocess_data(data_path):

    try:
        ## load data ##
        data = pd.read_json(data_path)

    except ValueError:
         print("File not found. Check the path variable and filename")
         exit()

if __name__ == '__main__':
    print('Preprocessing data...')
    #### Preparation ####
    file_path = path.abspath(__file__)  # full path of your script
    dir_path = path.dirname(file_path)  # full path of the directory of your script
    print(dir_path)
    json_file_path = path.join(dir_path, 'data/clean_data.json')  # absolute zip file path
    _preprocess_data(data_path=json_file_path)

This will get the absolute path of the file being executed. Refer to here for more info on __file__

Upvotes: 1

Related Questions