i'm using this code to get all files in a given folder. Is there a way to get only the folders ?
a = os.listdir('Tools')
os. listdir() returns everything inside a directory -- including both files and directories. A bit simpler: (_, _, filenames) = walk(mypath). next() (if you are confident that the walk will return at least one value, which it should.)
os. listdir() method gets the list of all files and directories in a specified directory.
Using os. os. mkdir() method in Python is used to create a directory named path with the specified numeric mode. This method raise FileExistsError if the directory to be created already exists.
import os.path dirs = [d for d in os.listdir('Tools') if os.path.isdir(os.path.join('Tools', d))]
To print only the folders
print os.walk(DIR_PATH).next()[1]
To print only the files
print os.walk(DIR_PATH).next()[2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With