"Subprocess.call" काम नहीं करता है। त्रुटि संदेश: "[WinError 193]% 1 वैध Win32 अनुप्रयोग नहीं है" [डुप्लिकेट]
मेरी स्क्रिप्ट को विभिन्न प्रकार की स्क्रिप्ट्स खोलनी पड़ती हैं (.exe, .py, .c, आदि ..) और इस लक्ष्य तक पहुँचने के लिए मैं इन दो निर्देशों का उपयोग करता हूँ:
os.chdir(FOLDER_PATH)
os.system("start "+SCRIPT_NAME)
कोड काम करता है, लेकिन जब भी os.system("start "+SCRIPT_NAME)
इसका उपयोग किया जाता है तो यह कंसोल विंडो दिखाता है। इसे छिपाने के लिए, मैं इंटरनेट पर पढ़ता हूं कि मुझे subprocess
मॉड्यूल का उपयोग करना है। मैंने इन आदेशों का उपयोग करने की कोशिश की, लेकिन वे काम नहीं करते हैं:
C:\test
λ ls -l
total 16
-rw-r--r-- 1 Admin 197121 13721 Oct 19 00:44 test.py
C:\test
λ python
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import subprocess
>>> from subprocess import CREATE_NO_WINDOW
>>>
>>> subprocess.call("test.py", creationflags=CREATE_NO_WINDOW)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 340, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
OSError: [WinError 193] %1 is not a valid Win32 application
>>>
मैं इस मुद्दे को कैसे हल कर सकता हूं?
जवाब
आप उपयोग कर सकते हैं call()
, लेकिन run()
एक नया संस्करण माना जाता है, और अधिक मजबूत।https://docs.python.org/3/library/subprocess.html जब तक आप अपने लक्ष्यों को प्राप्त करते हैं, तब तक आपको जो भी पसंद हो, उसका उपयोग करें।
#! /usr/bin/env python3
import subprocess as sp
cmd = ['echo', 'hi'] ## separate args in list. safer this way
response = sp .run( cmd, stdout=sp.PIPE, stderr=sp.PIPE, encoding='utf-8', creationflags=sp.CREATE_NO_WINDOW)
print( response.stderr, response.stdout )
cmd = 'echo hi' ## alternate string method
response = sp .run( cmd, stdout=sp.PIPE, stderr=sp.PIPE, shell=True, encoding='utf-8', creationflags=sp.CREATE_NO_WINDOW)
print( response.stderr, response.stdout )
यदि आप एन्कोडिंग निर्दिष्ट नहीं करते हैं, तो यह b'binary एन्कोडेड स्ट्रिंग्स लौटाता है '