2

I'm new to Python (not to scripting). On Windows in PowerShell I can write:

    PS C:\> (dir -Path C:\junk *.jpg -Recurse).FullName | C:\PS\scripts\EchoPipe.ps1

........

In shell: C:\junk\GIMP_TEST\LuminosityMask.JPG

........

Where EchoPipe.ps1 is:

    foreach($x in $input) { # Is anything  similar to $input in Python?
        Write-Output('In shell: '+$x)
    }
    Write-Output('... and now I can process them')

How to do this in Python?

Q1: The left side of the pipe can be written in this way:

    import os
    import re
    for d,x,fl in os.walk('C:\\junk'):
        for f in fl:
            if re.search(r'.jpg$', f, re.I):
                print(d+os.sep+f)

Is there a simpler way?

Q2: How to write the equivalent of EchoPipe.ps1 in Python? And, most importantly, how to run them using pipeing? Do I run it in a Command Prompt or in Python 3... Shell and how? Please, be very detailed.

Vilmos
  • 51
  • 4
  • It's not entirely clear what you're looking for. In the `os.walk` loop, you can collect a all filenames matching your regex as a list of strings, and then do something with them directly in Python (open, read, etc.) If you want to actually read from the standard input in a Python script, [there are a few good ways to do that](https://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin). What exactly are you trying to do with these filenames and/or files? – bnaecker Jan 23 '20 at 01:53

1 Answers1

0

foreach($x in $input) { # Is anything similar to $input in Python?

for x in sys.stdin:
    ...

will perform line-wise iteration of stdin, blocking when there is no data on the pipe. You can also use dedicated reading methods if you e.g. want larger blocks of data.

import os
import re
for d,x,fl in os.walk('C:\\junk'):
    for f in fl:
        if re.search(r'.jpg$', f, re.I):
            print(d+os.sep+f)

Is there a simpler way? Given what you're doing, fnmatch is more than sufficient and terser. Also you should use os.path.join to join paths.

import fnmatch
import os

for root, _, files in os.walk(...):
    for f in fnmatch.filter(files, '*.jpg'):
        print(os.path.join(root, f))

alternatively, and again given what you're doing, recursive globbing would be more than sufficient and much simpler than manually walking the entries:

import pathlib

for path in pathlib.Path('C:\\junk').glob('**/*.jpg'):
    print(path)
Masklinn
  • 34,759
  • 3
  • 38
  • 57
  • Thanks Masklinn for the improvements, I've already made the changes; not pathlib, walk() is more clear than '**'. – Vilmos Jan 30 '20 at 23:30