首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python中如何提取filepath(目录)的一部分?

在Python中如何提取filepath(目录)的一部分?

提问于 2018-03-02 00:15:11
回答 2关注 0查看 2.4K

我需要提取某个路径的父目录的名称。这就是它看起来的样子:c:\ stuff \ directory_i_need \ subdir \ file。我正在用使用directory_i_need。我创建了一个函数,它会给我一个所有文件的列表,然后:

代码语言:txt
复制
for path in file_list:
   #directory_name = os.path.dirname(path)   # this is not what I need, that's why it is commented
   directories, files = path.split('\\')

   line_replace_add_directory = line_replace + directories  
   # this is what I want to add in the text, with the directory name at the end 
   # of the line.

我该怎么做?

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档