balkon16
balkon16

Reputation: 1438

PowerShell Python command-line arguments

I'm trying to run a Python script in PowerShell. While using sys module I stumble upon a problem of getting the return value of a PowerShell function. The function in question is Date -f 'dd\/MM\/yyyy' that returns 14/03/2019 and I'd like that value to be used as an argument in the Python script. I've been able so far to get the literal arguments from the command line, e.g. text.

Upvotes: 1

Views: 1815

Answers (2)

Tomalak
Tomalak

Reputation: 338248

This Python script (see sys.argv docs):

import sys
print(sys.argv)

called like this in Powershell:

python .\test.py -date $(Date -f "dd\/MM\/yyyy")

outputs:

['.\\test.py', '-date', '14/03/2019']

On a general note, I recommend using better date formats than dd/MM/yyyy - ISO 8061 yyyy-MM-dd would be a good one.

Upvotes: 1

Heiko Becker
Heiko Becker

Reputation: 576

If I understand correctly, you want to have the output of the command Date -f 'dd\/MM\/yyyy' to be fed into your python script (script.py).

What you are looking for are so called "pipes" or output redirects. Looking at the officical documentation (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-output?view=powershell-6) the following might work:

$DATE=Date -f 'dd/\MM\/yyyy'
Write-Output $DATE | python script.py

Upvotes: 1

Related Questions