How to get the name of the directory where the file resides?

I've already asked a question on working with files in python. So. I need to get the name of the directory where is the file. Not the path of the form /home/path/to/file/filename.txtnamely, the directory name is /home/path/to/file/filename.txt.
How can I return from this function?
def my_list(path):
 paths = []
 for dirname, dirnames, filenames in os.walk(path):
 filenames = filter(lambda x: x.endswith('.html'), filenames)
 for filename in filenames:
 file = os.path.join(dirname, filename)
paths.append(file)
 return paths

I suppose that it would be best to remove it from dirname and return to the functions in the form of a dictionary. Like that. I need a pair of the form
{'file': '/home/path/to/file/filename.txt', ... }
def my_list(path):
 myDict = {}
 for dirname, dirnames, filenames in os.walk(path):
 filenames = filter(lambda x: x.endswith('.html'), filenames)
 for filename in filenames:
 parentdir = []
 filelist = []
 f = os.path.join(dirname, filename)
filelist.append(f)
 parentdir = ...
parentdir.append()
filelist.append(f)
 myDict = dict(zip(parentdir, filelist))
 return myDict

But how to do it, do not really understand.
PS it is very likely that there is a wild garden and bydlokod. But, much as they can. For any advice thanks.
June 14th 19 at 18:15
3 answers
June 14th 19 at 18:17
Solution
os.path.basename(os.path.dirname(s))

dirname cuts off the file name from the basename path selects the name of the desired directory
Thank you.
PS a digression. on a ten point scale how terrible my code?(( - elenora_Carroll commented on June 14th 19 at 18:20
June 14th 19 at 18:19
os.path
June 14th 19 at 18:21
dirLst=os.path.dirname(path).split("/")
dirname = dirLst[-1]

Find more questions by tags Python