in Technology by
How do I list all files of a directory?

1 Answer

0 votes
by
os.listdir() will get you everything that's in a directory - files and directories. If you want just files, you could either filter this down using os.path: from os import listdir from os.path import isfile, join onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))] or you could use os.walk() which will yield two lists for each directory it visits - splitting into files and dirs for you. If you only want the top directory you can break the first time it yields from os import walk f = [] for (dirpath, dirnames, filenames) in walk(mypath): f.extend(filenames) break or, shorter: from os import walk _, _, filenames = next(walk(mypath))

Related questions

0 votes
    How can we delete all files in a directory? (a) Files.delete(path) (b) Files.deleteDir() (c) Directory ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    How can we delete all files in a directory? (a) Files.delete(path) (b) Files.deleteDir() (c) ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    I'm working on this script that first takes all .csv's and converts them .xlsx's in a separate ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I'm working on this script that first takes all .csv's and converts them .xlsx's in a separate ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 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
    I want to take all the changes I made in the staging to my working directory. What command should I use to perform this ... B. git reset HEAD C. git reset TAIL D. git change...
asked Dec 23, 2022 in Technology by JackTerrance
0 votes
    How to list all the files in a commit?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    Select the 3 graphics files from the list below Choose all of the correct answers (multiple possibilities). Untitled ... jpg Unulled of Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    Which directory contain Cassandra configuration files (1)lib (2)conf (3)bin (4)interface...
asked Apr 17, 2021 in Technology by JackTerrance
0 votes
    In a certain city, 5% of all the persons in town have unlisted phone numbers. If you select 100 names at ... directory, how many people selected will have unlisted phone numbers?...
asked Feb 12, 2021 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
    Which class loader loads jar files from JDK directory? (a) Bootstrap (b) Extension (c) System (d) ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    How do you find a list of files that have changed in a particular commit?...
asked Nov 2, 2020 in Technology by JackTerrance
0 votes
    How do you find a list of files that have been changed in a particular commit?...
asked Oct 4, 2020 in Technology by Editorial Staff
0 votes
    Which is the parent of all the folders. Hard drive Root directory File Select the correct answer from above options...
asked Dec 11, 2021 in Education by JackTerrance
...