Tôi cần trích xuất tên của thư mục cha của một đường dẫn nhất định. Đây là những gì nó trông giống như:
c:\stuff\directory_i_need\subdir\file
Tôi đang sửa đổi nội dung của "tập tin" bằng một cái gì đó sử dụng directory_i_needtên trong đó (không phải đường dẫn). Tôi đã tạo một hàm sẽ cho tôi một danh sách tất cả các tệp, và sau đó ...
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.
Làm thế nào tôi có thể làm điều đó?