0

I'm having issues loading files by thier path over my code which runs over Windows (local test and development) and Linux (CI CD).

While running my code locally in Windows, the file path relative works fine, when my code is running over Linux it turns to a mess and returns an Error: No such file or directory

Is there such a code in Python which is cross platform to solve it ?

My code is like this:

def get_event_json_file_path(fileName):
    file_dir = os.path.dirname(os.path.realpath('__file__'))
    file_path = os.path.join(file_dir, "events/" + fileName)
    return file_path

Is there a code to get the classpath of the folder ?

9
  • Show us your code! Commented Apr 20, 2017 at 12:03
  • And of course, you can use the functions in os.path and below to platform-independently handle paths, make absolute ones relative, etc. Commented Apr 20, 2017 at 12:04
  • If you use relative paths and slashes (not backslashes) as separators, it should work on all operating systems. Commented Apr 20, 2017 at 12:04
  • code added, please look Commented Apr 20, 2017 at 12:06
  • Jesus christ... that answer where you copied os.path.dirname(os.path.realpath('__file__')) from should be deleted. Such a fundamental lack of understanding... Commented Apr 20, 2017 at 12:11

1 Answer 1

1

I managed to code this function:

def get_relative_file_path(file_dir_path, fileName):
    dir = os.path.dirname(__file__)
    file_path = os.path.join(dir, file_dir_path,fileName)
    return file_path

Usage:

get_relative_file_path('../resources/', "restCallBodySchema.json")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.