Search code examples
pythongithubherokudiscordcloud

heroku + github path error in the log - cannot find the file path


I deployed my github master branch to heroku. One of my code tries to access a file in the misc directory under the same branch. However, the log on heroku shows that the file does not exist. Please see the screenshot as follows:

enter image description here


Solution

  • You are using Windows-style backslashes as your path separators, e.g. something like this:

    with open("foo\\bar.txt") as f:
       ...
    

    or this:

    with open(r"foo\bar.txt") as f:
       ...
    

    Backslashes as path separators don't work on Linux.

    Use forward slashes instead, which works both on Linux and on Windows:

    with open("foo/bar.txt") as f:
       ...
    

    Better yet, use pathlib to join path segments:

    import pathlib
    
    directory = pathlib.Path("foo")
    with open(directory / "bar.txt") as f:
       ...
    

    Or os.sep:

    import os
    
    with open(os.sep.join("foo", "bar.txt")) as f:
       ...