Hello I am trying to split a path into directory and filename, on Windows. I am using the os.path.basepath API to accomplish that on Windows, but I am getting this error.
I tried upgrading pip3 followed by os_sys module, but still getting this error. Anyone can help me understand what is going on with os.path.basepath
on Windows 11?
I have exhausted google search and reading blogs over this, but nothing is working at this point.
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os.path
>>> os.path.basepath("c:\node\text.txt")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'ntpath' has no attribute 'basepath'
>>>
Two items:
if by 'basepath', you want the path, use dirname
(if you want to parent directory of text.txt
) or basename
if you just want the filename.
Use either \\
for windows paths or use a raw string.
i.e.
for dir:
import os
os.path.dirname("c:\\node\\text.txt")
for filename:
import os
os.path.basename("c:\\node\\text.txt")
or
for dir:
os.path.dirname(r'c:\node\text.txt')
for filename:
os.path.basename(r'c:\node\text.txt')
I apologize as I wasn't too clear on whether you want the dirname or filename.