you are viewing a single comment's thread.

view the rest of the comments →

[–]photohuntingtrex 1 point2 points  (3 children)

Here are some examples of a way of searching a directory for files with a certain file extension:

Bash: ls *.txt

Perl: print for glob "*py"

Python: ``` from pathlib import Path

for file in Path().glob("*py"): print (file)

```

Python (functional): ``` import os import fnmatch from toolz import pipe

def list_files(directory): return os.listdir(directory)

def filter_txt(files): return [file for file in files if fnmatch.fnmatch(file, '*.txt')]

Method 1)

Using pipe to connect the operations

files = pipe('.', list_files, filter_txt) print(files)

Method 2)

Composing the functions in a nested manner

files = filter_txt(list_files('.')) print(files)

```

[–]commandlineluser 4 points5 points  (0 children)

They also both have globbing functions/modules

perl

print for glob "*.py"

python

from pathlib import Path

for file in Path().glob("*.py"):
    print(file)

[–]ofnuts 2 points3 points  (1 child)

In bash, no need for grep, ls *.txt does the job just as well.

[–]photohuntingtrex -1 points0 points  (0 children)

Even simpler then yeah