简体   繁体   中英

How to loop through files in a folder and run a specific python code on them using powershell?

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 ocr3.py --image content
}

The ocr3.py file (with code $ python ocr3.py --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 ocr3.py --image $_.FullName } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM