Answering the question: to remove the last character, just use:string = string[:-1]
.
If you want to remove the last '\' if there is one (or if there is more than one):
while string[-1]=='\\':
string = string[:-1]
If it's a path, then use the os.path
functions:
dir = "dir1\\dir2\\file.jpg\\" #I'm using windows by the way
os.path.dirname(dir)
although I would 'add' a slash in the end to prevent missing the filename in case there's no slash at the end of the original string:
dir = "dir1\\dir2\\file.jpg"
os.path.dirname(dir + "\\")
When using abspath, (if the path isn't absolute I guess,) will add the current working directory to the path.
os.path.abspath(dir)