當前位置:首頁 » 編程語言 » pythondos命令

pythondos命令

發布時間: 2023-01-18 12:36:21

『壹』 python 怎麼執行dos命令

寫了幾個批處理,主要是一些Android調試命令,現在想用python來搞,感覺更酷一些吧。O(∩_∩)O~

比如Ping命令:

ping www..com

用python來做,主要是使用了python標准庫中的os庫。

參見Python文檔The Python Standard Library => Generic Operating System Services => os — Miscellaneous operating system interfaces.

這里介紹的比較全面,而且這個庫我們用的也較多。

我們會用到os.system()方法:

[python]view plain

  • importos

  • cmd='cmd.exe/kpingwww..com'

  • os.system(cmd)

『貳』 如何在pythonDos模式中進入D盤

按windows和r鍵打開運行界面,在運行界面輸入cmd,按回車進入命令提示符中,在命令提示符中輸入D:按回車即可進入D盤。
python怎麼設置dos對python中執行DOS命令的方法總結
使用os.system("cmd"),特點是執行的時候程序會打出cmd在linux上執行的信息。使用Popen模塊產生新的process,現在大部分人都喜歡使用Popen。Popen方法不會列印出cmd在linux上執行的信息。的確,Popen非常強大,支持多種參數和模式。使用前需要from。

『叄』 如何在Python中執行DOS命令

關於python調用cmd命令,主要介紹兩種方式:

一、Python的OS模塊

OS模塊調用CMD命令有兩種方式:os.system(),os.popen()。 都是用當前進程來調用。

1、os.system()

a、簡單粗暴的執行cmd指令

b、返回結果為0表示執行成功,無法獲取命令輸出的內容,本在cmd輸出的內容會直接在控制台輸出。

c、當命令運行結束後接著往下面執行程序(同步阻塞式)。用法如:os.system("ipconfig")。

2、os.popen()

a、能獲取DOS命令輸出的內容。

b、os.popen()返回的是一個file對象,那麼可以跟打開文件一樣操作,r是以讀的方式打開。

c、當命令運行結束後接著往下面執行程序(同步阻塞式)。

注意:os.popen() 方法用於從一個命令打開一個讀寫管道,在Unix,Windows中有效。

用法如:

with os.popen(r"adb devices","r")as f:

    text = f.read()

    print(text)# cmd輸出結果

二、管道subprocess模塊

a、在當前進程下產生子進程。

b、用wait()函數等待結果(非同步非阻塞式)。

【方法1】

result = subprocess.check_output(r'adb devices') ## 命令執行的輸出結果放到了result里

print(result)

【方法2】

process = subprocess.Popen(r'adb devices',shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

command_output = process.stdout.read().decode('gbk')

print(command_output)

Notices: 

有些命令行是非同步執行的不會馬上返回輸出,所以有時候我們要先等這個命令行執行完畢才能從stdout讀出來數據。這個時候要加上

process.wait()

我的GitHub

『肆』 Python自帶IDLE Shell和命令行(cmd中) 清屏方法

一、針對命令行CMD中:調用DOS系統提供的cls命令

import os

os.system('cls')

二、針對IDLE Shell

1、在...\Python\Lib\idlelib文件夾下新建ClearWindow.py文件

class ClearWindow:

    menudefs = [

        ('options', [None,

                    ('Clear Shell Window', '<<clear-window>>'),

                    ]), ]

    def __init__(self, editwin):

        self.editwin = editwin

        self.text = self.editwin.text

        self.text.bind("<<clear-window>>", self.clear_window2)

        self.text.bind("<<undo>>", self.undo_event)  # add="+" doesn't work

    def undo_event(self, event):

        text = self.text

        text.mark_set("iomark2", "iomark")

        text.mark_set("insert2", "insert")

        self.editwin.undo.undo_event(event)

        # fix iomark and insert

        text.mark_set("iomark", "iomark2")

        text.mark_set("insert", "insert2")

        text.mark_unset("iomark2")

        text.mark_unset("insert2")

    def clear_window2(self, event):  # Alternative method

        # work around the ModifiedUndoDelegator

        text = self.text

        text.undo_block_start()

        text.mark_set("iomark2", "iomark")

        text.mark_set("iomark", 1.0)

        text.delete(1.0, "iomark2 linestart")

        text.mark_set("iomark", "iomark2")

        text.mark_unset("iomark2")

        text.undo_block_stop()

        if self.text.compare('insert', '<', 'iomark'):

            self.text.mark_set('insert', 'end-1c')

        self.editwin.set_line_and_column()

    def clear_window(self, event):

        # remove undo delegator

        undo = self.editwin.undo

        self.editwin.per.removefilter(undo)

        # clear the window, but preserve current command

        self.text.delete(1.0, "iomark linestart")

        if self.text.compare('insert', '<', 'iomark'):

            self.text.mark_set('insert', 'end-1c')

        self.editwin.set_line_and_column()

        # restore undo delegator

        self.editwin.per.insertfilter(undo)

2、 在Python\Lib\idlelib目錄下編輯config-extensions.def(IDLE擴展配置文件)

在該文件最後增加如下內容:

[ClearWindow]

enable=1

enable_editor=0

enable_shell=1

[ClearWindow_cfgBindings]

clear-window=<Control-Key-w>    

--定義了清屏快捷鍵Ctrl + w,w必須是小寫

啟動Python IDLE,在Options菜單下會出現" Clear Shell Window Ctrl+W"

這時候就可以用快捷鍵Ctrl + w 清屏了

『伍』 python調用dos命令

換個實現方式吧,這種方式不需要cmd.exe,默認就會執行的:
import os
os.system(r"e: & cd e:\test & dir >> files.txt")

『陸』 python如何打開上次運行

1 直接命令行啟用Python。當然,如果直接在cmd中輸入python,需要在windows中的path環境變數中做好設置。此時,cmd中運行python就可以出現 「>>>」 符號。意味著python進入了交互運行模式,可在此模式下做簡單的python命令。2. 一種是用它自帶的開發環境IDLE。在開始運行處運行命令cmd,進行dos模式,輸入python,即可進行python的互動式環境。
3、互動式界面可以用於簡單的學習,編寫較大程序時應到具體的python文件中,python文件默認的後綴為.py,我們可以新建文本文件,然後把後綴擴展名改為.py,最後選擇菜單中的Run下的run mole即可運行,快捷鍵為F5。

『柒』 DOS 命令進入 python 目錄

把py.bat批處理文件改一下文件名再試一試。不要使用python類似的名字。
很可能是另有一個名為py.exe之類的可執行文件存在。

熱點內容
統治戰場kk腳本 發布:2025-07-17 11:53:45 瀏覽:239
安卓的數據線介面有什麼顏色 發布:2025-07-17 11:53:03 瀏覽:871
塑料壓縮器 發布:2025-07-17 11:52:15 瀏覽:724
手機百度雲如何秒上傳 發布:2025-07-17 11:46:27 瀏覽:612
交易貓買腳本靠譜嗎 發布:2025-07-17 11:32:27 瀏覽:520
伺服器系統盤一般做什麼陣列 發布:2025-07-17 11:16:05 瀏覽:363
如何用c語音實現放射密碼 發布:2025-07-17 11:13:03 瀏覽:735
lol腳本2017 發布:2025-07-17 10:52:18 瀏覽:827
編譯條件有哪些 發布:2025-07-17 10:42:51 瀏覽:334
apache設置靜態內容緩存時間 發布:2025-07-17 10:32:32 瀏覽:295