Hello, I am trying to run a script in Paraview’s Python shell and I need the relative path of several files within my script. If I use Python’s os library I will get the execution path and I won’t know the absolute path beforehand.
Thank you Mathieu. When I install the nightly build from here I get an error. When I run the latest nightly build I get an error saying “The code execution cannot proceed because VCOMP140.DLL was not found. Reinstalling the program may fix this problem”. I have tried reinstalling.
After getting the nightly build to work I can now use the __file__ specifier in my program. I could not do that with the published 5.7.0 build. Here is a short program I am using to test this.
import os
dir = os.path.dirname(__file__)
print(dir)
If I place my script into the execution directory (which is not what I ultimately want) C:\Program Files\ParaView 5.7.0-963-g032f608266-Windows-Python3.7-msvc2015-64bit I get the following output.
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name '__file__' is not defined
In the nightly build it limits me to having my script in the execution path which isn’t what I want. If I drop my script into some arbitrary directory \my\home\directory I get the error message I posted above.
File "<string>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in
position 2-3: truncated \UXXXXXXXX escape
This looks to be an issue because you have a path C:\Users which needs to be escaped for Python strings. \Us is not the beginning of a valid unicode escape sequence.
This block of code should print out the correct file path in the nightly build as of 01-13-20.
dir = os.path.dirname(__file__)
print(dir)
For some strange reason certain directories break this functionality and I cant seem to figure out why. Here is an example of testing the above two lines of code in two different directories, lets just call this script Path.py. Any directory starting with a numerical value will also break the functionality that I am looking for which isn’t to much of a concern for me.
File Location: C:\Test\Test1\Test2\Test3\Path.py
Output: C:\Test\Test1\Test2\Test3
Result: This works
File Location: C:\Test\Test1\run\Test3\Path.py
Output: C:\Test\Test1
un\Test3
Result: This Breaks
Here is the output in the Python shell.
I don’t believe that it is an issue with the unicode escape sequence because I’m not physically specifying the file path. I could be wrong and misinterpreting your answer. Regardless thank you for your response.
You can’t just type paths at a Python prompt (why this is working as is already confuses me). You need to make them strings with ' or ". And then, \ needs escaped (typed literally as \\) to show up. Python is replacing the \r in your \run bit with a carriage return here.
Okay thank you I understand where the error itself is coming from now, but I still don’t understand why its happening because I’m not just typing paths at the Python shell. I know that if I were manually specifying the directory name it would be dir = ‘C:\Test\Test1\Test2\Test3\Path.py’ but are you saying that I can’t just do dir = os.path.dirname(__ file __ ) to recover my file path? That line of code has always worked for me? Thank you for helping me figure this out.
__file__ should be fine in a script, but does not work at the shell; there is no file being executed there. That variable is also not defined at a Python REPL prompt which is what the Python shell is. If you’re literally typing it in as a string, you need to follow the escaping rules for the \ separators in your Windows path and double them up.