Showing posts with label Python Examples. Show all posts
Showing posts with label Python Examples. Show all posts

Sunday, May 18, 2025

List All Files From All Subdirectories - Python


import os

target = 'c:\\Python38-64'

for x in os.walk(target):
    print(x)

The os.walk() function generates the file names in a directory tree by walking the tree either top-down or bottom-up, using a generator. It returns a 3-tuple (dirpath, dirnames, filenames) for each directory in the tree, where dirpath is a string of the path to the directory, dirnames is a list of the names of the subdirectories in dirpath, and filenames is a list of the names of the non-directory files in dirpath.

os.walk(target) is used to generate and print the 3-tuples for each directory in the tree starting at target. The output will show the directory tree structure, with each directory and its subdirectories listed under the preceding directory, and the non-directory files listed under their respective directory. 

import os

target = 'c:\\Python38-64'

for root, dirname, files in os.walk(target):
    for x in files:
        print(x)

This code uses the os module to traverse a directory tree rooted at c:\\Python38-64. The os.walk() function returns a generator object that yields three-tuples for each directory in the tree. Each three-tuple consists of a directory path (root), a list of subdirectory names (dirname), and a list of file names (files) in the directory root.

The for loop iterates over each three-tuple yielded by the os.walk() generator. For each three-tuple, the loop iterates over each file name in the files list and prints it to the console. This effectively prints the name of every file in the c:\\Python38-64 directory tree. 


import os

target = 'c:\\Python38-64'

for root, dirname, files in os.walk(target):
    for x in files:
        print(root + '\\' + x)

This code uses the os.walk() function to iterate through all the directories and files in the specified target directory and its subdirectories.

The os.walk() function returns a generator that produces a 3-tuple on each iteration: the root directory of the current iteration, a list of subdirectory names in the current iteration, and a list of file names in the current iteration.

In this code, we are unpacking the 3-tuple into root, dirname, and files. root is the current root directory being iterated over, dirname is a list of subdirectory names in the current root directory, and files is a list of file names in the current root directory.

The for loop then iterates over each file name in files and prints out the full file path by concatenating root and the file name with the backslash (\\) character used as a directory separator on Windows. The resulting output is a list of all the files in the target directory and its subdirectories with their full paths. 

import os

target = 'c:\\Python38-64'

for root, dirname, files in os.walk(target):
    for x in files:
        if x.endswith('.py'):
            print(root + '\\' + x)

This code uses the os module to traverse the file system starting at the given target directory (c:\Python38-64) and walk through all directories and subdirectories within it. For each file found within this directory and its subdirectories, the code checks whether the filename ends with the extension .py. If the file has this extension, it prints out the full path to the file (using root and x). This code can be useful if you need to find all Python source code files within a particular directory hierarchy.

Thursday, May 1, 2025

Python Examples

Socket Programming - Python

Security & Networks - Python

File & System Operations - Python

MySQL Python Examples - Python

ZIP Examples - Python

Audio Programming - Python

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...