Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell wildcards in passing filenames as argument(s)

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.

like image 967
ddetts Avatar asked May 10 '17 15:05

ddetts


People also ask

Can you use wildcards in PowerShell?

You can use wildcard characters in commands and script blocks, such as to create a word pattern that represents property values.

What is $_ PowerShell?

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.


1 Answers

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
like image 104
Adil Hindistan Avatar answered Nov 15 '22 08:11

Adil Hindistan