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.

 Answers

71

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

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

os.walk(directory)

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:

next(os.walk('.'))[1]

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
 
Andres
answered 7 Months ago
15

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.

https://mywebsite.com/wp-content/themes/mytheme

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

Update:

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

PHP

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'"));
  die($results);
}

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
 
gMale
answered 7 Months ago
53
function filename_part($f) {
    return pathinfo($f, PATHINFO_FILENAME);
}

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

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.
        dirnames.remove('.git')
Tuesday, June 1, 2021
 
Tapha
answered 7 Months ago
47
data Color = Red
           | Yellow
           | Green
           deriving Enum

allColors = [Red ..]
Friday, July 23, 2021
 
kat_indo
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 :  
Share