0

Example: I have the main folder named "alphabets" under this i have 'n' number of subfolders. I want to extract the 'n' subfolder names into excel/or any file using python.

alphabets #main folder abc #subfolder1 def #subfolder2 . . . xyz #subfoldern

I want the output as : abc def . . . xyz

Aishwarya
  • 3
  • 2
  • Maybe this could help: [Getting a list of all subdirectories in the current directory](https://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory) – Jan Koci Apr 23 '20 at 14:00
  • Yes, I have reviewed that page earlier but it will give the whole sub-directory path not the sub-folder name. I have tried to split the path but couldnt achieve it. I just want to export only the sub-folder name. @JanKoci – Aishwarya Apr 23 '20 at 14:32

2 Answers2

0

Please take a look a python pathlib module and consider again Jan Koci answer. It should help you to get a directory name from a directory path.

os.path module can help you too by providing the method basename to a string representing a path.

Take a look also at os.path.split.

Regards.

cestMoiBaliBalo
  • 146
  • 2
  • 5
0

This is can be easily accomplished using os module.

here is an example code-

import os

path = "...\\xyz\\alphabets"
files = os.listdir(path)

with open('file_name.txt', 'a') as f:
    for ele in files:
        f.write(ele+'\n')
    f.close()
print('completed!!')
Pankaj Mishra
  • 445
  • 6
  • 15