我需要提取某個路徑的父目錄的名稱。這是它的樣子:c:\ stuff \ directory_i_need \ subdir \ file
。我正在修改「文件」的內容,其中使用了directory_i_need
名稱(不是路徑)。我創建了一個函數,它會給我一個所有文件的列表,然後...在Python中提取一部分文件路徑(一個目錄)
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.
我該怎麼做?
你可能想看看這個答案了:http://stackoverflow.com/a/4580931/311220 – Acorn 2012-04-13 23:01:27
上面的鏈接幫助我瞭解如何修復我做錯了什麼。謝謝。 – Thalia 2012-04-13 23:18:28