load Python Pickle (.pkl) file
Question: I am trying to load .pkl files that are in the same directory where my .py
file is located. The following is my code:

    
    
    import os
    
    def load_var(var_name):
        fid = open(os.path.join((var_name, '.pkl')))
        data = pickle.load(fid)
        fid.close()
        return data
    
    def main():
        data = load_var('myfilename')
    
    if __name__ == '__main__':
        main()
    

I keep on running into the error:

    
    
    fid = open(os.path.join((var_name, '.pkl')))
    TypeError: coercing to Unicode: need string or buffer, tuple found
    

Is there an easy way to resolve this error?


Answer: The input to `os.path.join` should not be a tuple. That is, the command should
read:

    
    
    fid = open(os.path.join(var_name, '.pkl'))
    

