Python won't accept file path when executed
Solution 1:
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__