Traversing all folders in Python

I want to view all the folders inside the directory:

directory\ folderA\ a.cpp folderB\ b.cpp folderC\ c.cpp folderD\ d.cpp 

The name of the folders is known. In particular, I am trying to count the number of lines of code in each of the source files a.cpp , b.cpp , c.pp and d.cpp . So, go inside folderA and read a.cpp , count the lines, and then go back to the directory, go to folderB , read b.cpp , count the lines, etc.

This is what I had so far,

 dir = directory_path for folder_name in folder_list(): dir = os.path.join(dir, folder_name) with open(dir) as file: source= file.read() c = source.count_lines() 

but I'm new to Python and have no idea if my approach is appropriate and how to proceed. Any sample code would be appreciated!

Also, does with open open / close a file, as it should be for all of these readings, or does it take more processing?

+1
source share
3 answers

I would do it like this:

 import glob import os path = 'C:/Users/me/Desktop/' # give the path where all the folders are located list_of_folders = ['test1', 'test2'] # give the program a list with all the folders you need names = {} # initialize a dict for each_folder in list_of_folders: # go through each file from a folder full_path = os.path.join(path, each_folder) # join the path os.chdir(full_path) # change directory to the desired path for each_file in glob.glob('*.cpp'): # self-explanatory with open(each_file) as f: # opens a file - no need to close it names[each_file] = sum(1 for line in f if line.strip()) print(names) 

Output:

 {'file1.cpp': 2, 'file3.cpp': 2, 'file2.cpp': 2} {'file1.cpp': 2, 'file3.cpp': 2, 'file2.cpp': 2} 

Regarding the with question, you do not need to close the file or do any other checks. You must be safe, as it is now.

You can check if full_path , since someone (you) could have mistakenly deleted the folder from your PC (folder from list_of_folders )

You can do this with os.path.isdir , which returns True if the file exists:

os.path.isdir(full_path)

PS: I used Python 3.

+3
source

Use Python 3 os.walk() to move all subdirectories and files of a given path, open each file and execute your logic. You can use the for loop to go through it, greatly simplifying your code.

https://docs.python.org/2/library/os.html#os.walk

+2
source

As the manglano said, os.walk ()

You can create a list of folders.

 [src for src,_,_ in os.walk(sourcedir)] 

You can create a list of file paths.

 [src+'/'+file for src,dir,files in os.walk(sourcedir) for file in files] 
+1
source

Source: https://habr.com/ru/post/1246827/


All Articles