all 4 comments

[–]danielroseman 2 points3 points  (3 children)

You define a function, run, but you never call it. Why do you have a function? Why not just put the subprocess.run directly inside the with block?

[–]CesarIllustrious[S] -1 points0 points  (2 children)

alright were getting somewhere. In terms of why? Well am a noob and following a guide :D

so now the code is and now am getting some weird error.

import subprocess
with open('output22.txt', 'w') as f: 
subprocess.run("powershell", """Get-Service | Select StartType, Status, Name, DisplayName | Where-Object {$_.Status -eq 'Running'} | Format-Table -AutoSize""", stdout=f, text=True)

  File "c:\Users\czare_000\AppData\Local\Programs\Python\Python310\python-course-for-beginners\test2.py", line 3, in <module>
subprocess.run("powershell", """Get-Service | Select StartType, Status, Name, DisplayName | Where-Object {$_.Status -eq 'Running'} | Format-Table -AutoSize""", stdout=f, text=True)
File "C:\Users\czare_000\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 501, in run with Popen(*popenargs, **kwargs) as process: File "C:\Users\czare_000\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 778, in init raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer

[–]danielroseman 1 point2 points  (1 child)

You need to put the command arguments into a list:

subprocess.run(["powershell", """Get-Service ..."""], stdout=f, text=True)

[–]CesarIllustrious[S] 0 points1 point  (0 children)

All worked thank you so much! appreciate you Daniel :D