python處理Windows平台上路徑有空格


  最近在采集windows上中間件的時候,遇到了文件路徑有空格的問題。

  例如:Aapche的安裝路徑為D:\Program Files\Apache Software Foundation\Apache2.2。

  采集apache要讀取配置文件D:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf

       執行一些D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe -v 這種命令。

  讀取配置文件是沒有問題的,因為用的是python代碼,打開文件,讀取文件,一行一行遍歷,用正則匹配或者字符串比較,就能獲取到信息,例如讀取配置信息獲取端口號。

          port_list=[]  
          with open(httpd_conf, "r") as f:
                file_list = f.readlines()
                regex = ur"^Listen\s*(\S*?:)*(\d+)\s*$"
                pattern_listener = re.compile(regex)
                for item in file_list:
                    listener_list = pattern_listener.findall(item)
                    if listener_list:
                        for port_info in listener_list:
                            if port_info:
                                port = port_info[1]
                                if port and port.strip():
                                    port_list.append(port.strip())            

   接下來說下,D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe -v 這種通過命令獲取信息的。

  httpd.exe -v 是獲取apache的版本信息。直接在在cmd命令行中輸入,顯示如下。 

D:\>D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe -v
'D:\Program' 不是內部或外部命令,也不是可運行的程序或批處理文件。

  有空格問題,搜了搜發現比較好的一種解決辦法,就是在把命令用雙引號引起來,下邊兩種寫法都可以。

D:\>"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -v
Server version: Apache/2.2.22 (Win32)
Server built:   Jan 28 2012 11:16:39

D:\>"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" "-v"
Server version: Apache/2.2.22 (Win32)
Server built:   Jan 28 2012 11:16:39

  接下來我們在python中用os.popen().read()試試怎么弄。

>>> import os
>>> cmd='"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -v'
>>> os.popen(cmd).read()  --這種寫法讀出來結果為空,是因為\要經過轉義,前邊加個r就行,cmd與cmd1區別
''
>>> cmd            --\b是正則表達式,所以變成了\x08 '"D:\\Program Files\\Apache Software Foundation\\Apache2.2\x08in\\httpd.exe" -v'
>>> cmd1=r'"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -v'
>>> cmd1
'"D:\\Program Files\\Apache Software Foundation\\Apache2.2\\bin\\httpd.exe" -v'

>>> os.popen(cmd1).read()
'Server version: Apache/2.2.22 (Win32)\nServer built:   Jan 28 2012 11:16:39\n'
>>>

   接下來再看一個比較復雜點的命令,httpd.exe" -V|find "Server MPM" 這個用來獲取apache的運行模式,windows下就是

WinNT,按剛才的套路在cmd命令行里執行沒問題。

D:\>"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -V|find "Server MPM"
Server MPM:     WinNT

  那么,我們繼續把他移植到python中,繼續用os.popen().read()。結果如下圖,都不出來結果。

  所以說,這種參數比較多的用這種方法是不行的。

>>> cmd1=r'"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -V|find "Server MPM" '
>>> os.popen(cmd1).read()
''
>>> cmd2=r'"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -V|find Server MPM ' >>> os.popen(cmd1).read() '' >>> cmd3=r'"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" "-V|find Server MPM" ' >>> os.popen(cmd1).read() ''

   在查閱相關資料后,可用subprocess.Popen()來代替os.popen()這個方法,

  但是執行后,出來的結果不是想要的,所以說這個方法也實現不了效果(如下)。

>>> import subprocess
>>> cmd=r'D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe -V|find "Server MPM"' >>> cmd 'D:\\Program Files\\Apache Software Foundation\\Apache2.2\\bin\\httpd.exe -V|find "Server MPM"' >>> ps = subprocess.Popen(cmd) >>> Server version: Apache/2.2.22 (Win32) Server built: Jan 28 2012 11:16:39 Server's Module Magic Number: 20051115:30 Server loaded: APR 1.4.5, APR-Util 1.4.1 Compiled using: APR 1.4.5, APR-Util 1.4.1 Architecture: 32-bit Server MPM: WinNT threaded: yes (fixed thread count) forked: no

  看到這樣的結果,放棄折騰了,最終選擇了一個曲線救國的方案,用python的os模塊,先進入到httpd.exe所在的目錄,之后,再執行命令。

>>> homepath="D:\Program Files\Apache Software Foundation\Apache2.2"
>>> BinPath = os.path.join(homepath, 'bin')
>>> os.chdir(BinPath)
>>> apache_model = os.popen('httpd.exe -V |find "Server MPM"').read()
>>> print apache_model
Server MPM:     WinNT

  

 

  參考文章:1.python中那糾結的os.system()與空格處理

       2.Python在通過os.system執行含有空格路徑

              3.http://www.it1352.com/540052.html

       4.python執行系統命令的方法:os.system(), os.popen(), subprocess.Popen()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM