I’m writing a command-line directory navigator for Windows in Python and struggling a bit with os.path.join. Here’s, in essence, what I’m trying to do:
abspath = "C:\Python32\Projects\ls.py"
abspath = abspath.split('\\')
print(abspath) #this prints ['C:', 'Python32', 'Projects', 'ls.py']
if(options.mFlag):
print(os.path.join(*abspath)) #this prints C:Python32\Projects\ls.py
m = time.ctime(os.path.getmtime(os.path.join(*abspath))) #this throws an exception
The problem is that os.path.join is not inserting a ‘/’ after ‘C:’ and I can’t figure out why. Any help?
Edit: In case anyone in the future comes here looking for a solution, I just added os.sep after “C:” instead of hardcoding a backslash and that worked.
From the documentation:
It’s a little hard to tell what you’re trying to accomplish, since all your code seems to be aiming at is to split the path and then put it back together exactly the way it was, in which case why split it in the first place? But maybe
os.path.splitdrivewill help you? It splits the drive letter from the path.