Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the last '/' or '\\' character in Python

Tags:

python

string

If I have a string that looks like either

./A/B/c.d

OR

.\A\B\c.d

How do I get just the "./A/B/" part? The direction of the slashes can be the same as they are passed.

This problem kinda boils down to: How do I get the last of a specific character in a string?

Basically, I want the path of a file without the file part of it.

like image 700
Clark Gaebel Avatar asked Dec 03 '25 11:12

Clark Gaebel


1 Answers

Normally os.path.dirname() is used for this.

like image 166
Ignacio Vazquez-Abrams Avatar answered Dec 05 '25 01:12

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!