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
#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


Messages In This Thread
homework - by Prani05 - Apr-30-2020, 08:56 AM
RE: homework - by anbu23 - Apr-30-2020, 09:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to print directory files (& timestamps) chatguy 3 1,428 Dec-29-2022, 07:19 AM
Last Post: ndc85430
  Opening files in same directory as script Emekadavid 3 2,627 May-28-2020, 06:50 PM
Last Post: Emekadavid
  Program that displays the number with the greatest amount of factors ilusmd 3 2,899 Nov-01-2018, 08:28 PM
Last Post: ichabod801
  Creating a variable that displays time groovydingo 3 2,985 Apr-17-2018, 04:46 AM
Last Post: tannishpage
  Homework that displays different weights Schmitlab 4 3,399 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