Python: Get system command output
Python Basic: Exercise-102 with Solution
Write a Python program to get system command output.
Sample Solution:-
import subprocess# file and directory listing
returned_text = subprocess.check_output("dir", shell=True, universal_newlines=True)
print("dir command to list file and directory")
print(returned_text)
dir command to list file and directory
磁碟區 F 中的磁碟是 KINGSTON
磁碟區序號: 6098-37A4
F:\Python_APSC 的目錄
2017/12/22 下午 10:42 <DIR> .
2017/12/22 下午 10:42 <DIR> ..
2018/01/09 下午 08:51 4 test.txt
2017/12/22 下午 11:19 2,304 a017-2.py
2017/12/22 下午 11:23 1,585 a017-3.py
2018/01/10 下午 02:19 206 py-ex-basic-77.py
2018/01/08 下午 10:29 277 py-ex-basic-31.py
2018/01/10 下午 02:23 133 py-ex-basic-78.py
2018/01/08 下午 10:31 239 py-ex-basic-32.py
2018/01/10 下午 02:27 481 py-ex-basic-79.py
2018/01/09 上午 08:17 221 py-ex-basic-33.py
2018/01/10 下午 02:30 110 py-ex-basic-80.py
2018/01/09 上午 08:18 173 py-ex-basic-34.py
2018/01/10 下午 02:33 129 py-ex-basic-81.py
2018/01/09 上午 08:21 219 py-ex-basic-35.py
2018/01/10 下午 02:38 68 py-ex-basic-82.py
2018/01/09 上午 09:46 216 py-ex-basic-36.py
2018/01/10 下午 02:42 129 py-ex-basic-83.py
2018/01/09 下午 02:05 193 py-ex-basic-37.py
2018/01/10 下午 02:46 114 py-ex-basic-84.py
2018/01/09 下午 02:08 100 py-ex-basic-38.py
2018/01/10 下午 02:48 244 py-ex-basic-85.py
2018/01/09 下午 02:11 114 py-ex-basic-39.py
2018/01/10 下午 03:09 79 py-ex-basic-86.py
2018/01/09 下午 02:24 124 py-ex-basic-40.py
2018/01/10 下午 03:06 125 py-ex-basic-87.py
2018/01/09 下午 02:36 275 py-ex-basic-41.py
2018/01/09 下午 02:36 0 abc.txt
2018/01/09 下午 02:29 0 abc1.txt
2018/01/11 下午 01:32 199 py-ex-basic-101.py
2018/01/11 下午 01:49 205 py-ex-basic-102.py
221 個檔案 142,412 位元組
2 個目錄 11,107,581,952 位元組可用
>>>
沒有留言:
張貼留言