Apr-29-2020, 05:05 AM
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.
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.
Apr-29-2020, 05:18 AM
You have hint, why don't you use it?
I would do:
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). /.../
Apr-30-2020, 08:56 AM
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.
import os if _name_ == "_main_": for files in os.walk("."): if .py in files: print(files)I tried this but am getting error
Apr-30-2020, 09:34 AM
__name__
and __main__
- with 2 underscores in front and back.There are other problems too - probably you want
.py
in quotes?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.
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 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)