Avatar ·

टर्मिनल आउटपुट को वेरिएबल में रीडायरेक्ट करना

📁 पायथन, प्रश्न, उदाहरण

CMD आउटपुट को वेरिएबल में रीडायरेक्ट करने को समझने में मदद करें, क्योंकि वेरिएबल को केवल प्रोसेस का Exit_code असाइन किया जाता है। स्पष्टता के लिए, कुछ उदाहरण दिए गए हैं: उदाहरण 1

import sys, os command = ""tasklist"" result1 = os.system(command) print(result1) # आउटपुट पर 0

उदाहरण 2

command = ""tasklist > temp.txt"" result2 = os.system(command) print(result2) # आउटपुट पर 0 with open(''temp.txt'', 'r') as f: result2 = f.read() print(result2) # फ़ाइल से आउटपुट पढ़ें