Python Forum
traverses directory recursively and displays files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: traverses directory recursively and displays files (/thread-26348.html)



traverses directory recursively and displays files - Prani05 - Apr-29-2020

Write a program which traverses the current working directory recursively and displays those files ending with .py.
Hint : Use walk method of os package.


RE: traverses directory recursively and displays files - buran - Apr-29-2020

Welcome to the forum. We are glad to help, but we are not going to do your homework for you.
Please, post your code in python tags, full traceback if you get any - in error tags. Ask specific questions.

See BBcode help for more info.

Also, use descriptive thread titles. I changed this one for you.


RE: traverses directory recursively and displays files - perfringo - Apr-29-2020

You have hint, why don't you use it?

I would do:

>>> import os
>>> help(os.walk)
Help on function walk in module os:

walk(top, topdown=True, onerror=None, followlinks=False)
    Directory tree generator.
    
    For each directory in the directory tree rooted at top (including top
    itself, but excluding '.' and '..'), yields a 3-tuple
    
        dirpath, dirnames, filenames
    
    dirpath is a string, the path to the directory.  dirnames is a list of
    the names of the subdirectories in dirpath (excluding '.' and '..').
    filenames is a list of the names of the non-directory files in dirpath.
    Note that the names in the lists are just names, with no path components.
    To get a full path (which begins with top) to a file or directory in
    dirpath, do os.path.join(dirpath, name).

/.../



homework - Prani05 - Apr-30-2020

Write a program which traverses the current working directory recursively and displays those files ending with .py.
Hint : Use walk method of os package.


import os 
if _name_ == "_main_": 
    for files in os.walk("."):
         if .py in files:
            print(files)
I tried this but am getting error


RE: traverses directory recursively and displays files - buran - Apr-30-2020

__name__ and __main__ - with 2 underscores in front and back.
There are other problems too - probably you want .py in quotes?


RE: homework - anbu23 - Apr-30-2020

(Apr-30-2020, 08:56 AM)Prani05 Wrote: Write a program which traverses the current working directory recursively and displays those files ending with .py.
Hint : Use walk method of os package.


import os 
if _name_ == "_main_": 
    for files in os.walk("."):
         if .py in files:
            print(files)
I tried this but am getting error

os.walk() returns output in 3-tuple and third element has files list. Use the third element in if statment

You have to enclose string in quotes.
if '.py' in ...



RE: traverses directory recursively and displays files - Prani05 - Apr-30-2020

I tried its not working


RE: traverses directory recursively and displays files - snippsat - Apr-30-2020

Use always 3 temporary variables to address the 3-tuple that os.walk() yields.
import os

for root, dirs, files in os.walk('.'):
    for file in files:
        if '.py' in file:
            print(file)