Python Forum
traverses directory recursively and displays files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
traverses directory recursively and displays files
#1
Write a program which traverses the current working directory recursively and displays those files ending with .py.
Hint : Use walk method of os package.
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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).

/.../
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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
Reply
#5
__name__ and __main__ - with 2 underscores in front and back.
There are other problems too - probably you want .py in quotes?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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 ...
Reply
#7
I tried its not working
Reply
#8
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to print directory files (& timestamps) chatguy 3 1,319 Dec-29-2022, 07:19 AM
Last Post: ndc85430
  Opening files in same directory as script Emekadavid 3 2,513 May-28-2020, 06:50 PM
Last Post: Emekadavid
  Program that displays the number with the greatest amount of factors ilusmd 3 2,772 Nov-01-2018, 08:28 PM
Last Post: ichabod801
  Creating a variable that displays time groovydingo 3 2,857 Apr-17-2018, 04:46 AM
Last Post: tannishpage
  Homework that displays different weights Schmitlab 4 3,266 Oct-21-2017, 10:39 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020