gpt-3.5-turbo-0125· OpenAI
WildChat-4.8M (AI2)
I have a folder with some other folders in. Inner folders may contain txt-files. I want…
- User
I have a folder with some other folders in. Inner folders may contain txt-files. I want to know how lines include each of files. And it have to write a number of lines with file's path, starting with the root folder. The folder is called "test"
- Assistant
Here is a possible solution using Python:
import os def count_lines(root_folder): for foldername, _, files in os.walk(root_folder): for file in files: if file.endswith(".txt"): file_path = os.path.join(foldername, file) with open(file_path, 'r') as f: num_lines = sum(1 for line in f) print(f"{file_path}: {num_lines} lines") root_folder = "test" count_lines(root_folder)This script will recursively search through the "test" folder and count the number of lines in all
.txtfiles it finds, printing out the file path and number of lines for each file.