Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Loop to open multiple folders and files in python

I am new to python and currently work on data analysis.

I am trying to open multiple folders in a loop and read all files in folders. Ex. working directory contains 10 folders needed to open and each folder contains 10 files.

My code for open each folder with .txt file;

file_open = glob.glob("home/....../folder1/*.txt")

I want to open folder 1 and read all files, then go to folder 2 and read all files... until folder 10 and read all files. Can anyone help me how to write loop to open folder, included library needed to be used?

I have my background in R, for example, in R I could write loop to open folders and files use code below.

folder_open <- dir("......./main/")
for (n in 1 to length of (folder_open)){
    file_open <-dir(paste0("......./main/",folder_open[n]))

    for (k in 1 to length of (file_open){
        file_open<-readLines(paste0("...../main/",folder_open[n],"/",file_open[k]))
        //Finally I can read all folders and files.
    }
}
like image 575
Tiny_Y Avatar asked Mar 06 '18 16:03

Tiny_Y


1 Answers

This recursive method will scan all directories within a given directory and then print the names of the txt files. I kindly invite you to take it forward.

import os

def scan_folder(parent):
    # iterate over all the files in directory 'parent'
    for file_name in os.listdir(parent):
        if file_name.endswith(".txt"):
            # if it's a txt file, print its name (or do whatever you want)
            print(file_name)
        else:
            current_path = "".join((parent, "/", file_name))
            if os.path.isdir(current_path):
                # if we're checking a sub-directory, recursively call this method
                scan_folder(current_path)

scan_folder("/example/path")  # Insert parent direcotry's path
like image 122
GalAbra Avatar answered Oct 04 '22 15:10

GalAbra