I am trying to list out all of the sub-directories in a given path (see my code)
however, I am very new to python and am wondering what would be the best approach
the below code will produce everything in the folder but I am looking for the directories and sub-directories only
import os
def main():
videosDir = os.listdir("D:\TempServer\Videos\Movies")
for dir in videosDir:
dirName = "" + dir
print(dirName)
if __name__ == '__main__':
main()
any help would be appriecated
Thanks!
littlejiver
Instead of os.listdir
use os.walk
. That will distinguish the folders from the files. It loops only on the folders, so you don't even need to filter out the non-folders. If you want the file names, there is a list of the files in each folder (and another list for the subfolders).
for folderName, subfolders, filenames in os.walk("D:\TempServer\Videos\Movies"):
print(folderName)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.