1

When I run

path = "C:\windows\System32\wininit.exe"
sub = (subprocess.run(["powershell.exe", "Get-Filehash", path, "-Algorithm", "md5"], capture_output=True))

I get this error: screenshot of output from "subprocess.run"

So I tried:

path = r"C:\windows\System32\wininit.exe"
path = "C:/windows/System32/wininit.exe"
path = r"C:/windows/System32/wininit.exe"

Still get the same error, and I have run out of options. please help me fix this, when I run it it always shows that it is trying to run 'c:\\windows\\system32\\winit.exe' even though it should only be 1 backslash.

Update: I should mention that: The code works for other paths however, not for all. And for the ones that do not work, I try to run it in the powershell individuall by Get-Filehash path -Algorithm md5 and it works, but just not when I run it with Python. Because this is the case, I think the code works but for some files it does not, although it should. So I think I need a solution for that!

Prab
  • 464
  • 3
  • 13
  • 1
    For the output, it is un-escaping the slash (converting to to 2 slashes). You can see it is un-escaping the newlines also (\r\n). `subprocess` is using a single slash in it's execution. To see the formatted message, pass the string to the 'print` function. – Mike67 Sep 19 '20 at 18:03
  • Does this answer your question? [Why do backslashes appear twice?](https://stackoverflow.com/questions/24085680/why-do-backslashes-appear-twice) – wjandrea Sep 19 '20 at 18:15
  • It appears you are using PowerShell to run an MD5 file hash. If so, why not just use the built-in `hashlib` library? – S3DEV Sep 19 '20 at 18:25
  • @Mike67 yes i understand that there is only 1 backslash, and the issue I am having is caused by un-escaping but how do I fix it is the issue. wjandrea yes that explains the how backslash functions in a string but I am not able to understand how to fix it tho. S3DEV you are right I could just use that, but when i just tried it, apparently i have to encode the path, which means the hash of the file will be changed and is no good for me! – Prab Sep 19 '20 at 18:44
  • Your code works for me with no changes. Do you have access to wininit.exe? – Mike67 Sep 19 '20 at 20:42
  • @Mike67 yes the code works for me too but only for some paths but not all. And yes I do have access to the file, the reason I can say that is because if I run the code in PowerShell with the same exact path, and command it generates the hash for me no problem. but just an issue when I run it with python. I also tried to run the script in administrative mode as well but still nothing. – Prab Sep 19 '20 at 20:57

1 Answers1

-2

Perhaps try using 2 backslashes, like so:

path = "C:\\windows\\System32\\wininit.exe"
  • No, there's no difference. In all current versions of Python, `"\w\S" == '\\w\\S'`, though this is being deprecated. – wjandrea Sep 19 '20 at 18:22