Asked  7 Months ago    Answers:  5   Viewed   41 times

Is there a way to return a list of all the subdirectories in the current directory in Python?

I know you can do this with files, but I need to get the list of directories instead.



Do you mean immediate subdirectories, or every directory right down the tree?

Either way, you could use os.walk to do this:


will yield a tuple for each subdirectory. Ths first entry in the 3-tuple is a directory name, so

[x[0] for x in os.walk(directory)]

should give you all of the subdirectories, recursively.

Note that the second entry in the tuple is the list of child directories of the entry in the first position, so you could use this instead, but it's not likely to save you much.

However, you could use it just to give you the immediate child directories:


Or see the other solutions already posted, using os.listdir and os.path.isdir, including those at "How to get all of the immediate subdirectories in Python".

Tuesday, June 1, 2021
answered 7 Months ago

If you check my old answers, I have already fixed this issue for you as well.

Proper way:

wp_enqueue_script('myscript', get_template_directory_uri() . '/assets/js/myjsfile.js', array('jquery'), false, true);

Use get_template_directory_uri() to get the correct URI of the active theme so that it will point to the root of your theme.

If you are using a child theme, use get_stylesheet_directory_uri().


// JavaScript Document
    type: 'post',
    url: my_ajax.ajax_url,
    // add your action inside data object
    data: { 
      action: 'waitlist_update' 
    success: function(data){
        // callback function


add_action('wp_ajax_waitlist_update', 'update_function');
function update_function() {
  global $wpdb;
  $results = $wpdb->query( $wpdb->prepare("UPDATE 'my_table' SET `currentstatus` = 'myupdate1' WHERE wdt_ID = '1'"));

wp_ajax_waitlist_update the waitlist_update is the action that ajax will trigger from your JavaScript. update_function is the function that will be called after the Ajax is triggered.

Saturday, May 29, 2021
answered 7 Months ago
function filename_part($f) {
    return pathinfo($f, PATHINFO_FILENAME);

$result = array_map("filename_part", scandir($directory));
Saturday, May 29, 2021
answered 7 Months ago

This is a way to traverse every file and directory in a directory tree:

import os

for dirname, dirnames, filenames in os.walk('.'):
    # print path to all subdirectories first.
    for subdirname in dirnames:
        print(os.path.join(dirname, subdirname))

    # print path to all filenames.
    for filename in filenames:
        print(os.path.join(dirname, filename))

    # Advanced usage:
    # editing the 'dirnames' list will stop os.walk() from recursing into there.
    if '.git' in dirnames:
        # don't go into any .git directories.
Tuesday, June 1, 2021
answered 7 Months ago
data Color = Red
           | Yellow
           | Green
           deriving Enum

allColors = [Red ..]
Friday, July 23, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :