Write a program which traverses the current working directory recursively and displays those files ending with .py.
Hint : Use walk method of os package.
Hint : Use walk method of os package.
traverses directory recursively and displays files
|
Write a program which traverses the current working directory recursively and displays those files ending with .py.
Hint : Use walk method of os package.
Apr-29-2020, 05:09 AM
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
Apr-29-2020, 05:18 AM
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.
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 __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
Apr-30-2020, 09:41 AM
(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. os.walk() returns output in 3-tuple and third element has files list. Use the third element in if statmentYou have to enclose string in quotes. if '.py' in ...
Apr-30-2020, 09:50 AM
I tried its not working
Apr-30-2020, 10:25 AM
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) |
|
Possibly Related Threads… | |||||
Thread | Author | Replies | Views | Last Post | |
![]() |
How to print directory files (& timestamps) | chatguy | 3 | 2,108 |
Dec-29-2022, 07:19 AM Last Post: ndc85430 |
Opening files in same directory as script | Emekadavid | 3 | 3,298 |
May-28-2020, 06:50 PM Last Post: Emekadavid |
|
Program that displays the number with the greatest amount of factors | ilusmd | 3 | 3,677 |
Nov-01-2018, 08:28 PM Last Post: ichabod801 |
|
Creating a variable that displays time | groovydingo | 3 | 3,812 |
Apr-17-2018, 04:46 AM Last Post: tannishpage |
|
Homework that displays different weights | Schmitlab | 4 | 4,076 |
Oct-21-2017, 10:39 AM Last Post: gruntfutuk |