Не получается соединить пути при помощи os.path.join()
Пытаюсь соединить пути в системе при помощи os.path.join() У меня есть класс, в который приходит параметр '\icons' И он ни как не присоединяется к пути root в self.sorting_path
class SortingFiles:
def __init__(self, sorting_pat):
self.root_path = os.getcwd()
self.sorting_path = os.path.join(self.root_path,sorting_pat)
self.end_path = os.path.join(self.root_path, 'icons_by_year')
self.files = {}
print(self.files)
print(self.sorting_path)
print(self.root_path)
Вот что, выводят принты:
{}
C:/icons
C:/Users/bluntwave/PycharmProjects/python_base/lesson_009
Ответы (1 шт):
Автор решения: Tr1nks
→ Ссылка
Ваша проблема в лидирующем слеш - такой путь join начинает строить от корня первого пути, т.е. если убрать лидирующий слеш (вместо \icons передать icons) то все будет работать.
import os
root_path=r'c:\dummy\root\path'
path_with_slash=r'\target'
path_without_slash='target'
print(os.path.join(root_path,path_with_slash)) # c:\target
print(os.path.join(root_path,path_without_slash)) # c:\dummy\root\path\target