Page 1 of 1

get basename from path

PostPosted: Fri Oct 12, 2018 8:51 am
by madmatt
Hi all,
I' m getting mad trying to figure out how can i get the filename of a file from a specific path.
I can't use folder dat, i have a specific path of a file and i need to get the basename.
Something like the os module can do in python.
There should be some implementation of that since the folder dat can do that.
But i cant figure out how can i do that with my specific code.

Re: get basename from path

PostPosted: Sat Oct 13, 2018 2:44 am
by Qvoloid
Hey there,

Can you elaborate a little bit more? You say:

I can't use folder dat, i have a specific path of a file and i need to get the basename.


Why can't you use folder? You can select in the name format the option "No extension" to have the basename of a file on the folder you are pointing to.

You also say:
Something like the os module can do in python.


Well, then why not using the os package? Is trivial to point to a path and getting its basename:

Code: Select all
import os

# Get basename of a file
os.path.basename(path)


Best,
Darien

Re: get basename from path

PostPosted: Mon Oct 15, 2018 12:05 pm
by rob
And as a small tip, if you want to include that all as a single expression in a parameter:

Code: Select all
__import__('os').path.basename(path)

Re: get basename from path

PostPosted: Tue Oct 16, 2018 4:16 pm
by nettoyeur
Nice one Rob, didn't know that!

Re: get basename from path

PostPosted: Fri Oct 19, 2018 11:02 am
by rob
Should add __import__() is a built-in Python function.

Inside TouchDesigner however, you can use the shorter:

Code: Select all
mod.os.path.basename(path)


mod (or 'Module On Demand') searches DAT operators, then if not found, external python modules:

https://docs.derivative.ca/MOD_Class

Re: get basename from path

PostPosted: Sat Oct 20, 2018 3:22 am
by Qvoloid
Fantastic! Didn't know that either. Thanks Rob!