Home > Software engineering >  Match folder names
Match folder names

Time:12-09

My code:

path = 'c:/users/student/pythonprojects'
path = os.realname(path) # A function that converts it to the real path names
print(path)

Expected output:

>>> 'C:/Users/Student/PythonProjects'

I don't want it to just capitalize the words. I want it to convert to the real folder names in the computer.

Thanks.

CodePudding user response:

It's not a trivial question, as path are case sensitive you can have multiple matches (FiLe.txt, FILE.txt, file.txt) can all exists in the same directory.

If you don't have such issues you can always compare name in lower case if they match use the correct path.

Use os.path.split() and os.path.splitdrive() to separate component of the path and check recursively each path from the drive letter to the file.

CodePudding user response:

Unfortunately, there is no such function as os.realname() in Python. However, you can use the os.path.realpath() function to convert a relative path to an absolute path. For example:

import os
path = 'c:/users/student/pythonprojects'
path = os.path.realpath(path)
print(path)
  • Related