0

I'm trying to execute a cmd command in my python script.

But there is some ' "" ' in the cmd commande and I'm using the os.system python library which breaks the ' "" ' of the Python command.

def open_HashMyFiles():
 os.system(".\HashMyFiles.exe /MD5 1 /SHA1 1 /SHA256 1  /CRC32 0 /SHA512 0 /SHA384 0 /SaveDirect /folder "C:\Users\PycharmProjects\pythonProject\WinHASH_py" /scomma "oktier.csv"")

What syntax should I use to make this work?

I tried :

os.system('
os.system([
os.system(`
os.system({
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
akmot
  • 63
  • 8
  • 2
    Correct would be __not__ using [os.system](https://docs.python.org/3/library/os.html#os.system) being deprecated since many years and should not be used anymore in newly written Python scripts, but use instead the [subprocess module](https://docs.python.org/3/library/subprocess.html). In this case can be used simply `subprocess.run()`. Wrong is also referencing an executable to run like `HashMyFiles.exe` with a path relative to __current directory__ because of the process starting `python.exe` to interpret the Python script defines the current working directory which can be any directory. – Mofi Aug 03 '22 at 16:32
  • If the executable `HashMyFiles.exe` is in same directory as the Python script file coded by you, there can be used [pathlib.Path(__file__).parent.resolve()](https://stackoverflow.com/a/3430395/3074564) to get the directory with the Python script file currently running and concatenate that path with `HashMyFiles.exe` to get the fully qualified file name of the executable to run with `subprocess.run()` with using additionally perhaps also `cwd=` and the directory path which defines the current working directory for `HashMyFiles.exe` on running it. – Mofi Aug 03 '22 at 16:37
  • 1
    I recommend to read the Microsoft documentation for the Windows kernel library function [CreateProcess](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) and the [STARTUPINFO](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) structure. Every Python programmer writing a Python script using the `subprocess` module instead of `os.system()` for running a Windows executable should know how processes are created on Windows and what is the current directory for each started process. – Mofi Aug 03 '22 at 16:39
  • By the way: `os.system()` uses also `CreateProcess` but without Python script programmer having any control over the parameters and always starting `%ComSpec%` which on Windows usually expands to `C:\Windows\System32\cmd.exe` with the argument `/c` and the command line defined in the string literal appended as additional arguments. If that arguments list is syntactically correct for Python and for `cmd.exe` (two script interpreters making it more difficult to use the correct syntax), `cmd.exe` uses also `CreateProcess` to run the executable. So `cmd.exe` is not needed on using `subprocess.run` – Mofi Aug 03 '22 at 16:43
  • PS: I recommend to read also the Python documentation for [Built-in Types](https://docs.python.org/3/library/stdtypes.html) (especially the section about __Text Sequence Type — str__), the chapter [String and Bytes literals](https://docs.python.org/3/reference/lexical_analysis.html#literals-1) and [Unicode HOWTO](https://docs.python.org/3/howto/unicode.html). – Mofi Aug 04 '22 at 09:42

2 Answers2

1

You can use single quotes arround and make your string a raw-string as you got unescaped backslashes in your command:

def open_HashMyFiles():
 os.system(r'.\HashMyFiles.exe /MD5 1 /SHA1 1 /SHA256 1  /CRC32 0 /SHA512 0 /SHA384 0 /SaveDirect /folder "C:\Users\PycharmProjects\pythonProject\WinHASH_py" /scomma "oktier.csv"')
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
1

You need to use the backslash escape character \. If you need a " in your string and you want to use " as the string delimiter, you'll need to "escape" your inner quotation mark.

# Don't use this code, see below for a better version of this
def open_HashMyFiles():
 os.system(".\HashMyFiles.exe /MD5 1 /SHA1 1 /SHA256 1  /CRC32 0 /SHA512 0 /SHA384 0 /SaveDirect /folder \"C:\Users\PycharmProjects\pythonProject\WinHASH_py\" /scomma \"oktier.csv\"")

You need to be careful with this escape character in your file paths - if one of your folders starts with n and you put a \ to symbolize accessing the folder, it will combine into a \n and become a new line! Usually for these cases we use the r string flag to prevent unnecessary escape sequences from working.

def open_HashMyFiles():
 os.system(r".\HashMyFiles.exe /MD5 1 /SHA1 1 /SHA256 1  /CRC32 0 /SHA512 0 /SHA384 0 /SaveDirect /folder \"C:\Users\PycharmProjects\pythonProject\WinHASH_py\" /scomma \"oktier.csv\"")

With the r flag, the only escape sequences that will work are to escape the quotation characters. Useful if you use \ in your string but don't need newlines.

But possibly the easiest way is to use single quotes if you aren't strict about what string delimiter is used. The r flag works on this too.

def open_HashMyFiles():
 os.system(r'.\HashMyFiles.exe /MD5 1 /SHA1 1 /SHA256 1  /CRC32 0 /SHA512 0 /SHA384 0 /SaveDirect /folder "C:\Users\PycharmProjects\pythonProject\WinHASH_py" /scomma "oktier.csv"')
Lakshya Raj
  • 1,669
  • 3
  • 10
  • 34