나의 목적은 디렉토리와 서브 디렉토리에 대응하는리스트로부터 함수를 만드는 것이다.디렉토리와 무제한의 서브 디렉토리를 만드는 함수
예 : 'reports/English'는 'reports'디렉토리의 하위 디렉토리 'English'에 해당합니다. 나는 폴더에 자신을 잃을 두려움 기능 os.chdir을 사용하지 않으
for i in lst:
splitted = i.split('/')
if not os.path.exists(destination_directory + '\\' + splitted[0]) :
os.mkdir(destination_directory + '\\' + splitted[0])
os.mkdir(destination_directory + '\\' + splitted[0] + '\\' + splitted[1])
else :
os.mkdir(destination_directory + '\\' + splitted[0] + '\\' + splitted[1])
:
여기 내 기능입니다. 내가 할 경우
lst_1 =
['music',
'reports/English',
'reports/Spanish',
'videos',
'pictures/family',
'pictures/party']
:
def my_sub_function(splitted):
"""
"""
if splitted == []:
return None
else:
if not os.path.exists(destination_directory + '\\' + splitted[0]) :
os.mkdir(destination_directory + '\\' + splitted[0])
os.mkdir(destination_directory + '\\' + splitted[0] + '\\' + splitted[1])
else :
os.mkdir(destination_directory + '\\' + splitted[0] + '\\' + splitted[1])
return t1(splitted[1:])
그래서,이 목록을 고려하십시오
내가 재귀 함수를 할 싶습니다, 나는이 시도
it will creates these directories :
.\\music
.\\reports\\English
.\\reports\\Spanish
.\\videos
.\\pictures\\family
.\\pictures\\party
을하지만, 나는 디렉토리와 단 하나의 하위 디렉토리로 제한되어있다. 내 기능은 3 개 또는 4 하위 디렉토리를 처리하는 것이 이런 식으로 뭔가를 만들 수 있도록 내가 좋아하는 것 :
.\\pictures\\family\\Christmas\\meal\\funny
사람이 아이디어를 가지고 있습니까?
감사합니다.
"작동하지 않는다"는 것은 좀 더 자세히 설명 할 수 있습니까? 뭐하는거야? – glibdud
['os.makedirs()'] (https://docs.python.org/3/library/os.html#os.makedirs)를 찾고 계십니까? – glibdud
내 게시물의 모호함 때문에 유감스럽게 생각합니다. 지금은 더 분명해졌습니다. – Manoa