file_path = (pathlib.Path(file).parent.resolve() / ‘/home/phaines/climate/data/idlist.dat’)
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘file’ is not defined
The data files are in /home/phaines/climate/data and not where the python is being run.
You cannot use __file__ in code that you run at the interpreter prompt. The entire point of __file__ is to say where the actual file for the current module exists (in this case, so that the code can look in places relative to that file to find a data file to open). But if you use the interpreter prompt, there is no such file (that’s the point).
The reason people write code like that is because relative paths look for files relative to the current working directory, which is not necessarily anything to do with where the Python files actually are. You must properly understand this concept in order to write correct code with relative paths - you cannot just copy someone else’s example and hope for the best.
However, in this case, you have an absolute path anyway (the path /home/phaines/climate/data/idlist.dat), which starts with a /). It already tells you exactly where in the filesystem to find the file, without caring about the current working directory. Therefore, you should not attempt to do any of these tricks. You should just use that path directly.