in Education by
How do I get a list of all files (and directories) in a given directory in Python? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
To traverse every file and directory in a directory tree you can use the following piece of code:- import os for dirname, dirnames, filenames in os.walk('.'): for subdirname in dirnames: print(os.path.join(dirname, subdirname)) for filename in filenames: print(os.path.join(dirname, filename)) The first for loop in the above code prints the path to all subdirectories. The second for loop in the code prints path to all filenames. Another way by which you can list all the Directory trees in Python is by using the following piece of code:- os.listdir(path)

Related questions

0 votes
    In the os module in Python, is there a way to find if a directory exists, something like: os.direxists(os ... in pseudocode True/False Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    How to copy a file in python? I already read this but found nothing. Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    In Python What command can I use to find these: Location of file which I am executing, and Current directory ( ... script in Python) Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I check the file going to be written exists in the directory, and if not , How can I create directory ... " flag for the same? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can I delete a file or a folder in Python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can I check if a key exists in a directory in Python, before updating the value of the key. I tried this ... better way to do this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I have two fields a string field and a numeric field in a directory. The key of the dictionary as well as the ... based on the keys? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can I find the size of a directory, using Hadoop? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to find all the files in directory with .txt extension in Python? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    How can I extract the decision path as a textual list from a trained tree in a decision tree ? Something similar to ... then class='A' Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Here is the problem I have a requirements.txt that looks like: BeautifulSoup==3.2.0 Django==1.3 Fabric==1.2.0 ... . So what went wrong? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I am facing problems in _files which isn't defined if I execute the script with exec, ececfile and in _module_ ... help me with this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    What will be the output of the following Python function? min(max(False,-3,-4), 2,7) a) -4 b) -3 c) 2 d) False...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    I can use vi{ and va{ to select C++ code blocks. It helps me a lot when I need to yank ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I have two list that I combine into a single list. Later I want to make a copy of this list so ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
...