+ 1

How to use in the correct way quotations marks in os.system() arguments?

(Hi, thanks in advance for your time! :))))) Well, I was trying to execute some .bat file in python. And I found the function os.startfile() that can let you to run every tipes of file: so for example, It can be ----> os.startfile("path_of_the_.bat) Searching for a while, I found that I can do the same using os.system(), where for argument I can put directly the batch command that there's in my .bat Here problems comes 'cause there're quotes! If your command is simple, you can alternate single and double quates, but what should I do if my command is complicate? In fact, I have a os.system() function and his argument that should be ----> powershell (Add-Type '[DllImport(\"user32.dll\")]^public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::SendMessage(-1,0x0112,0xF170,2) Have you an idea to make It possible without errors? (The code should make your screen go to spleep mode)

6th Mar 2019, 7:41 AM
Giordano Fratti
Giordano Fratti - avatar
3 odpowiedzi
+ 2
Did you tried using ''' //your commands here ''' I don't know what your code would do but I got that it's something related to double and single quotes which is raising problems. If I'm not wrong then using triple quoting should work for you.
6th Mar 2019, 9:30 AM
Шащи Ранжан
Шащи Ранжан - avatar
+ 2
I have just found the solution and I feel soooo stupid. Explained: 1) import re 2) put r before your command, so it won't give you problem with / and \ -----> os.system(r"command") 3) if It makes confusion with quotes, you easly should put diffent quotation's kinds or, as your last chance, use ''' and '''
11th Mar 2019, 10:41 PM
Giordano Fratti
Giordano Fratti - avatar
+ 1
It works syntactically, but then It gives me an error with ----> ...(\"user32.dll\")... (you can check it in the complete code above). In fact \ " and \" become orange from the interpreter, and they aren't read like a cmd command!
6th Mar 2019, 3:51 PM
Giordano Fratti
Giordano Fratti - avatar