I am running Python 3
on a Windows machine using PowerShell
. I am trying to execute a Python file and then pass a number of files (file1.html, file2.html, etc.) as arguments using a wildcard character. I can get this to work, performing a couple of steps like below:
PS $files = Get-Item .\file*.html
PS python mypythonfile.py $files
My question is can this be done without having to use Get-Item
and assigning the results to a variable? I tried running the same file like this python mypythonfile.py .\file*.html
but this results in an error from the Python interpreter because PowerShell doesn't parse out the wildcard and passes the string with the wildcard.
You can use wildcard characters in commands and script blocks, such as to create a word pattern that represents property values.
The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.
It seems that you are in the interactive console. You do not need to assign the results of get-item to a variable, if that is all you are trying to achieve. Try this:
python mypythonfile.py (get-item .\file*.html)
Although this will work, you should really be passing Python the name of the file properly by using .FullName property of the resulting objects generated by get-item:
python mypythonfile.py (get-item .\file*.html).FullName
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With