0

We are creating a python program that executes specific macros within Polyworks based on user input into the program. Right now the code is:

roto.command.CommandExecute('MACRO EXEC("C:\\RotoWorks\\Macros\\CentrifugalCompressor")')

However this assumes that our program is always installed in C:\RotoWorks. Ideally, our app is portable. I'm sure theres a way to retrieve the filepath that Rotoworks is stored in, then just concatenate the rest of the filepath to the end. How do I do this?

1

1 Answer 1

2

You can retrieve the path from the __file__ attribute of the file. Use os.path.abspath on that attribute to retrieve the absolute path of the file and then os.path.dirname to retrieve the containing directory:

import os

file_directory = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(file_directory, other_path) # join directory to an inner path
roto.command.CommandExecute('MACRO EXEC({})'.format(path))

Use os.path.dirname recursively to move out as many directories as you want.

Sign up to request clarification or add additional context in comments.

3 Comments

I used and it worked when running as a python script. But when I packaged it into an executable using py2exe, it says 'file is not defined'. How do I avoid this?
@RBuntu That would require a slightly different approach. This: How do I get the path of the current executed file in python?
Thanks! I'll check it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.