Only in Python 3.7 using subprocessing methods run
, Popen
etc. or even using os.system()
The PowerShell Script I'm calling seems to terminate before completion. I have a much more complex script, but I have been able to simplify both the Python script and PowerShell script to exhibit the issue. The PowerShell Script is also shown below.
Here is the Python code:
''' subprocess truncates output '''
import subprocess # subprocess library
p = subprocess.run("powershell -ExecutionPolicy ByPass -File C:/PS/testAquire.ps1",
shell=True, check=True, stdout=subprocess.PIPE)
print("stdout:", p.stdout)
print("stderr:", p.stderr)
This is the Python script output showing no errors:
stdout: b''
stderr: None
This is the entire contents of the result.txt
file after execution using the Python script above.
Directory: C:\windows\system32\drivers
Mode LastWriteTime Length Name
---- ------------- ------ ---- d----- 4/12/2018 5:15 AM en-US
d----- 4/12/2018 5:15 AM UMDF
-a---- 1/19/2017 5:43 AM 3301 1028_Dell_INS_24-7459.mrk
-a---- 4/11/2018 7:34 PM 29696 afunix.sys
-a---- 4/11/2018 7:34 PM 3440660 gm.dls
-a---- 4/11/2018 7:34 PM 646 gmreadme.txt
-a---- 9/12/2015 4:59 AM 18720 IntelMEFWVer.dll
This is the PowerShell Script
<#
.synopsis
Sample script to demonstrate unusual results when called from Python 3.7
#>
Get-ChildItem c:/windows/system32/drivers/ | Format-Table | Out-File c:/PS/result.txt -Encoding ascii
This is the normal output of the script when run from the PowerShell Command or when using Python 2.7 to launch the PowerShell script.
Directory: C:\windows\system32\drivers
Mode LastWriteTime Length Name
---- ------------- ------ ---- d----- 4/11/2018 7:38 PM DriverData
d----- 8/15/2018 5:33 PM en-US
d----- 5/30/2018 9:31 AM etc
d----- 11/14/2018 5:32 PM UMDF
d----- 12/10/2018 5:50 PM wd
-a---- 1/19/2017 5:43 AM 3301 1028_Dell_INS_24-7459.mrk
-a---- 4/11/2018 7:33 PM 237568 1394ohci.sys
-a---- 4/11/2018 7:33 PM 107416 3ware.sys
-a---- 4/11/2018 7:33 PM 654232 acpi.sys
-a---- 4/11/2018 7:33 PM 20480 AcpiDev.sys
-a---- 4/11/2018 7:33 PM 127904 acpiex.sys
-a---- 4/11/2018 7:33 PM 12800 acpipagr.sys
-a---- 4/11/2018 7:33 PM 14848 acpipmi.sys
-a---- 4/11/2018 7:33 PM 13824 acpitime.sys
-a---- 4/11/2018 7:33 PM 1135520 adp80xx.sys
-a---- 4/11/2018 7:34 PM 626592 afd.sys
-a---- 4/11/2018 7:34 PM 39424 afunix.sys
-a---- 4/11/2018 7:34 PM 108032 agilevpn.sys
-a---- 4/11/2018 7:34 PM 254464 ahcache.sys
-a---- 9/24/2015 5:17 AM 109200 aksdf.sys
-a---- 9/24/2015 5:17 AM 205528 aksfridge.sys
-a---- 4/11/2018 7:33 PM 181760 amdk8.sys
... ... I reduced the output for brevity ...
-a---- 4/11/2018 7:34 PM 33184 WppRecorder.sys
-a---- 4/11/2018 7:34 PM 23040 ws2ifsl.sys
-a---- 4/11/2018 7:33 PM 23040 WSDPrint.sys
-a---- 4/11/2018 7:33 PM 25088 WSDScan.sys
-a---- 4/11/2018 7:34 PM 125440 WUDFPf.sys
-a---- 4/11/2018 7:34 PM 264192 WUDFRd.sys
-a---- 6/15/2018 12:44 AM 295424 xboxgip.sys
-a---- 4/11/2018 7:33 PM 46592 xinputhid.sys
I should note I have tried dozens of methods to launch the script with the same results.
Did you try to run the subprocess without using a shell? Like so:
subprocess.run(
["powershell", "-ExecutionPolicy", "ByPass", "-File", "C:/PS/testAquire.ps1"],
shell=False,
)
Alternatively, if you prefer to make use of the PowerShell, why not use it to redirect the output into the destination file (simple add a redirection to the end of the command; ... > c:/output.file
)
Thanks for looking into this. I uninstalled Python 3.7.2 and then installed 3.6.8 and it works as designed. I have not determined if the problem was 3.7.2 or something to do with the installation. I will investigate.
Again, thanks for all your help and suggestions.
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.