I assumed something like that and should have said you want to use os.path.split() for that. Also os.path.splitext()
>>> import os
>>> p=r'C:\path\to\file\my name.csv'
>>> os.path.split(p)
('C:\\path\\to\\file', 'my name.csv')
>>> os.path.splitext(p)
('C:\\path\\to\\file\\my name', '.csv')
I usually do something like this. A lot of tasks need to convert a file from say a TIFF to a PDF so I often need to cleanly pull the extension off of the filename and then put the new file in a new place...
def myfunc(pathname, targetdi):
(path, filename) = os.path.split(pathname)
(name, ext) = os.path.split(filename)
newname = os.path.join(targetdir, name, '.csv')
It's also completely OS independent since there are no slashes or upper or lower case checks or drive letters involved. I usually ignore checking for really weird invalid paths, and use "try" blocks to catch those or just let it fail and fix the problems as the come up.
Sometimes on big jobs I use a "try" block catch all the weird missing or broken files in a list and print them at the end, it's faster to process say 10000 files and then look at a list of 5 broken ones than it is to run the job 5 times dealing with one broken file at a time.