1

I'm currently making an autosave function for a program based in python, and I have very little knowledge of python. I remember learning how to cut, but this is a bit more of an advanced cut. Right now, I have it printing me the path file in string format (no I cannot use os.path or anything like that) and what I want, is for it to remove the entire path except for NAME.pse(The name will change as well). Here is an example path and ultimately what I'd like it to look like, but I would like for it to work with any path that it prints out so it has compatibility with anyone's computer in any file structure, along with any name of the session file (the .pse):

C:/Users/Install/OneDrive/B&BLab/Coding/TestingCell/PyMol.pse => PyMol.pse

2 Answers2

0

You can use the split() function to split the string at all / characters. This will return a list, then just take the last element of that list:

myString = "C:/Users/Install/OneDrive/B&BLab/Coding/TestingCell/PyMol.pse"
myFile = myString.split('/')[-1]

However, Python does provide a function for this. Check out this answer.

amitchone
  • 1,630
  • 3
  • 21
  • 45
0

If you want only the filename:

print("".join(stringa.split('/')[-1:]))

And if you want also the containing folder(s):

print("/".join(stringa.split('/')[-2:]))
shamalaia
  • 2,282
  • 3
  • 23
  • 35