亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決python3中os.popen()出錯(cuò)的問題

 更新時(shí)間:2020年11月19日 11:27:54   作者:小妮淺淺  
在本篇文章里小編給大家整理的是一篇關(guān)于解決python3中os.popen()出錯(cuò)的問題的相關(guān)內(nèi)容,有興趣的朋友們可以參考下。

使用程序難免會(huì)有出錯(cuò)的時(shí)候,如何從大篇代碼中找出錯(cuò)誤,不僅考驗(yàn)?zāi)芰?,還要考驗(yàn)小伙們的耐心。辛辛苦苦敲出的代碼運(yùn)行不出結(jié)果,非常著急是可以理解的。那么我們在python3中使用os.popen()出錯(cuò)該怎么辦?本篇文章小編同樣以錯(cuò)誤的操作方法為大家進(jìn)行講解,一起找尋不對的地方吧。

在當(dāng)前 desktop 目錄下,有如下內(nèi)容:

desktop $ls
client.py  server.py  中文測試
arcpy.txt  codetest.py  test.py

如上所示:有一個(gè)中文命名的文件 ----> 中文測試

# -*- coding:utf-8 -*-
# python3.5.1
import os,sys
print (sys.getdefaultencoding()) #系統(tǒng)默認(rèn)編碼
dir_list = os.listdir()
for li in dir_list:
print (li)

輸出如下:

utf-8
arcpy.txt
client.py
codetest.py
server.py
test.py
中文測試

可以看出默認(rèn)編碼為 utf-8,os.listdir()命令可以正常輸出中文字符。

 在使用 os.popen()時(shí):

# -*- coding:utf-8 -*-
# python3.5.1
import os,sys
print (sys.getdefaultencoding()) #系統(tǒng)默認(rèn)編碼
dir_list = os.popen('ls','r').read()
for li in dir_list:
print (li)

報(bào)錯(cuò)如下:

utf-8

Traceback (most recent call last):

File "Desktop/codetest.py", line 8, in <module>

dir_list = os.popen('ls','r').read()

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/ascii.py", line 26, in decode

return codecs.ascii_decode(input, self.errors)[0]

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 76: ordinal not in range(128)

解決:

命令行執(zhí)行沒有問題,這個(gè)是編輯器的事。建議用subprocess

到此這篇關(guān)于解決python3中os.popen()出錯(cuò)的問題的文章就介紹到這了,更多相關(guān)python3中os.popen()使用出錯(cuò)怎么辦內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論