How To Loop Through Files In A Folder And Run A Specific Python Code On Them Using Powershell?

- 1 answer

I have written python code for performing OCR on an image file. The python code involves argparse and needs to be run via powershell. However, I am able to run this code on only 1 image at a time. I want to write a powershell loop to loop through images in a specific folder and run the python code. I am using Windows 10 and python 3. I never used powershell before.

I have tried using the Foreach-Object command over a folder with code as shown below:

Get-ChildItem "Path to the folder with pics"  -Filter *.jpg | Foreach-Object {
       $content = Get-Content $_.FullName
       python --image content

The file (with code $ python --image images\image.jpg) returns a word document with the recognised text. I was expecting that the above loop to return a bunch of word documents one for each of the images in the folder



There is no requirement to use get-content if you just need to pass a filepath as an argument. Try the following instead:

Get-ChildItem "C:\Temp\pics" -Filter *.jpg | Foreach-Object { python --image $_.FullName }