我有这样的文件结构:
d:\temp\random1\index.html
d:\temp\random2\index.html
d:\temp\random3\index.html
Run Code Online (Sandbox Code Playgroud)
我想获得在python中列出的路径.所以输出将是:
files = ['path': 'd:\temp\random1\index.html', 'directory': 'random1']
Run Code Online (Sandbox Code Playgroud)
我正在使用这样的代码:
files = []
for dirpath, dirnames, filenames in os.walk('D:\\temp'):
for fname in filenames:
if fname.endswith(".md"):
path = os.path.join(dirpath,fname)
files.append({'path':path,'directory': dirpath})
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何获取目录值.我得到的代码是:
files = ['path': 'd:\temp\random1\index.html', 'directory': 'd:\temp\random1\']
Run Code Online (Sandbox Code Playgroud)
如何在没有一些脏黑客的情况下获取目录?