Is there any meaning for triple underscores in this python for a loop?

Is there any value in triple underscore in Python?

This was in a script to get all the files .txtin a directory.

for ___,___,files in os.walk(some_folder):
    files[:]=[x for x in files if x.lower().endswith('txt')]
    for file in files:

If you read other questions, is here one underscore character usually used to throw out variables, is using triple underscore just bad practice or does it matter to him?

+4
source share
2 answers

, , , , script , ( , "", ).

, , (.. Python ___ -, foo), .

, - __ ___. ___ __ ; , - _; _, .

+5

, ___ , , (___), . , :

test :

test
    subtest1
        file1.txt
        file2.txt
    subtest2
        file3.txt

, , os.walk test:

>>> list(os.walk('test'))
[('test', ['subtest1', 'subtest2'], ['.DS_Store']), ('test/subtest1', [], ['file1.txt', 'file2.txt']), ('test/subtest2', [], ['file3.txt'])]
>>> len(list(os.walk('test')))
3

, , os.walk('test'). , ?

>>> [len(x) for x in os.walk('test')]
[3, 3, 3]

, os.walk('test') . , list(os.walk('test'))[0] 'test' ( ), ['subtest1', 'subtest2'] ( ) ['.DS_Store'] ( ). , ( .DS_Store - test).

for, ( ):

>>> for main_dir, sub_dirs, files in os.walk('test'):
...         print("Main directory: ", main_dir)
...         print("Sub-directories: ", ', '.join(sub_dirs))
...         print("Files: ", ', '.join(files))
... 
Main directory:  test
Sub-directories:  subtest1, subtest2
Files:  .DS_Store
Main directory:  test/subtest1
Sub-directories:  
Files:  file1.txt, file2.txt
Main directory:  test/subtest2
Sub-directories:  
Files:  file3.txt

for main_dir, sub_dirs, files in os.walk('test') - , "". main_dir os.walk('test'), sub_dirs .. ___ ( _), : " , , ". : " , , , ".

+3

Source: https://habr.com/ru/post/1673117/


All Articles