Reputation: 547
This Question Already have a solution but In my case I'm not getting the correct solution where am I getting wrong?
import os,sys
filename = "C:\Users\Dell\Desktop\ProjectShadow\app2\aapp2s.py"
directory, module_name = os.path.split(filename)
module_name = os.path.splitext(module_name)[0]
print(module_name)
print(directory)
Insterd I want
>>
aapp2s
C:\User\Dell\Desktop
What's Wrong ?
Upvotes: 2
Views: 1126
Reputation: 12168
Try pathlib
:
from pathlib import PureWindowsPath
filename = r"C:\Users\Dell\Desktop\ProjectShadow\app2\aapp2s.py"
p = PureWindowsPath(filename)
module_name = p.stem
directory = p.parents[2]
print(module_name)
print(directory)
out:
aapp2s
C:\Users\Dell\Desktop
Upvotes: 0
Reputation: 161
either use r"C:\Users\Dell\Desktop\ProjectShadow\app2\aapp2s.py"
or you can double backshlash the whole thing "C:\\Users\\Dell\\Desktop\\ProjectShadow\\app2\\aapp2s.py"
The strange thing you see on your print is the result of the \a escape char
Upvotes: 2