get list of folders in directory python
import os
my_list = os.listdir('My_directory')
get list of folders in directory python
import os
my_list = os.listdir('My_directory')
python get list of files in directory
from os import listdir
file_list = listdir(folder_path)
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']
get the list of file names from a folder in python
from os import listdir
from os.path import isfile, join
# Getting file path from user and store it on path variable
path = input("Please enter a directory name:\n")
# Try catch for printing error message if the user enter invalid path
try:
# Getting list of file from the given path and store it in (onlyfiles) variable
onlyfiles = [f for f in listdir(path) if isfile(join(path, f))]
# Looping through the file and print each file
for file in onlyfiles:
print(file)
except Exception as e:
# If there is some error, print error message
print("Incorrect path.")
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us