Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

get list of folders in directory python

import os 
my_list = os.listdir('My_directory')
Comment

list only directories

# List directories under the current working directory.
ls -l | grep "^d"

# List directories under the current working directory and 
# include hidden folders
ls -la | grep "^d"
Comment

list directory in python

from os import listdir

## Prints the current directory as a list (including file types)
print(os.listdir())
Comment

python list only files not directories

files = (file for file in os.listdir(path) 
         if os.path.isfile(os.path.join(path, file)))
for file in files: # You could shorten this to one line, but it runs on a bit.
    ...
Comment

python list directories only

>>> [ name for name in os.listdir(thedir) if os.path.isdir(os.path.join(thedir, name)) ]
['ctypes', 'distutils', 'encodings', 'lib-tk', 'config', 'idlelib', 'xml', 'bsddb', 'hotshot', 'logging', 'doc', 'test', 'compiler', 'curses', 'site-packages', 'email', 'sqlite3', 'lib-dynload', 'wsgiref', 'plat-linux2', 'plat-mac']
Comment

find only list directories

find . -maxdepth 1 -type d -iname "<regex>" -printf '%f
'
Comment

python3 list directories

import os

arr = os.listdir("c:/temp/")

print("
".join(arr))
Comment

PREVIOUS NEXT
Code Example
Python :: python png library 
Python :: convert pandas.core.indexes.numeric.int64index to list 
Python :: How to Adjust Title Position in Python 
Python :: pandas separator are multiple spaces 
Python :: python crear dataframe 
Python :: python argument parser default value 
Python :: beautifulsoup import 
Python :: how to cut image python 
Python :: python generator example 
Python :: pyqt5 qtextedit change color of a specific line 
Python :: prime numbers python 
Python :: apply on dataframe access multiple columns 
Python :: ForeignKey on delete django 
Python :: iterate through a list 
Python :: python delete first two indexes 
Python :: record audio with processing python 
Python :: create a 2d array in python 
Python :: flask dockerize 
Python :: how to create a virtual environment in python 
Python :: react-native-dropdown-picker 
Python :: python single line if 
Python :: r named chr to dataframe 
Python :: opencv load image python 
Python :: epoch to gmt python 
Python :: get unique values from a list 
Python :: python get numbers after decimal point 
Python :: raspistill timelapse 
Python :: python list replace nan with 0 
Python :: flask session auto logout in 5 mins 
Python :: python print date, time and timezone 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =