Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python program to traverse directories and read file information

I'm just getting started with Python but already have found it much more productive than Bash shell scripting.

I'm trying to write a Python script that will traverse every directory that branches from the directory I launch the script in, and for each file it encounters, load an instance of this class:

class FileInfo:

    def __init__(self, filename, filepath):
        self.filename = filename
        self.filepath = filepath

The filepath attribute would be the full absolute path from root (/). Here's the pseudocode mockup for what I'd like the main program to do:

from (current directory):

    for each file in this directory, 
    create an instance of FileInfo and load the file name and path

    switch to a nested directory, or if there is none, back out of this directory

I've been reading about os.walk() and ok.path.walk(), but I'd like some advice about what the most straightforward way to implement this in Python would be. Thanks in advance.

like image 694
dvanaria Avatar asked Mar 24 '11 15:03

dvanaria


People also ask

How to traverse a directory in Python?

Traversing a directory means get all files or subdirectories in it. To do it, we should know: In this tutorial, we will write an example to show you how to traverse a directory in python. entry.is_dir () and entry.is_file () is the key.

How to iterate over files in a directory in Python?

We can iterate over files in a directory using Path.glob () function which glob the specified pattern in the given directory and yields the matching files. Path.glob (‘*’) yield all the files in the given directory

How to retrieve the files of a directory in Python?

The scandir () function was included in the standard Python library in Python 3.5 back in September 2015. Here’s an example of using the function to retrieve the files of a directory: The glob module is also a part of the standard Python library. Users can utilize the module to find the files and folders whose names follow a specified pattern.

What is a directory in Python?

Directory also sometimes known as a folder are unit organizational structure in a system’s file system for storing and locating files or more folders. Python as a scripting language provides various methods to iterate over files in a directory.


2 Answers

I'd use os.walk doing the following:

def getInfos(currentDir):
    infos = []
    for root, dirs, files in os.walk(currentDir): # Walk directory tree
        for f in files:
            infos.append(FileInfo(f,root))
    return infos
like image 73
John Percival Hackworth Avatar answered Oct 30 '22 16:10

John Percival Hackworth


Try

info = []
for path, dirs, files in os.walk("."):
    info.extend(FileInfo(filename, path) for filename in files)

or

info = [FileInfo(filename, path)
        for path, dirs, files in os.walk(".")
        for filename in files]

to get a list of one FileInfo instance per file.

like image 27
Sven Marnach Avatar answered Oct 30 '22 15:10

Sven Marnach