Python os.path.exists()函數(shù)總是返回false的解決方案
如下面所示,如果我們用file的readline或readlines,在每一行后面都有一個(gè)n回車(chē)符
直接os.path.exists(readline)時(shí)總會(huì)返回false
>>> from os.path import exists>>> exists(’dog.png’)True>>> exists(’dog.pngn’)False
使用item.strip(’n’) #前面的item為我定義的變量
去掉后再傳遞給os.path.exists(item) 就OK了。
補(bǔ)充:當(dāng)os.path.exists(path)的path中包含有空格時(shí)返回結(jié)果為False的解決方案
之前有個(gè)問(wèn)題一直沒(méi)有解決, 當(dāng)路徑中或文件名中存在空格時(shí),用os.path.exists(path)判斷是否存在時(shí),都會(huì)返回False. 百思不得其解. 今天在用ipython偶到想到想了解一下到底是什么原因?
事實(shí)上,當(dāng)用input()接收path輸入時(shí),path中有空格時(shí),生成的str是不一樣的. 如下:
In [4]: path = input(’請(qǐng)將文件拖入:’)
請(qǐng)將文件拖入:'C:UsersxxxxxDesktopfilename with space.txt'
In [5]: pathOut[5]: ’'C:UsersxxxxxDesktopfilename with space.txt'’ In [6]: path1 = input(’請(qǐng)將文件拖入:’)
請(qǐng)將文件拖入:C:UsersxxxxxDesktopfilenamewithspace.txt
In [7]: path1Out[7]: ’C:UsersxxxxxDesktopfilenamewithspace.txt’ In [8]: os.path.exists(path)Out[8]: False In [9]: os.path.exists(path1)Out[9]: True
很明顯,帶有space時(shí)生了的str多了一層''字符串,故將多余的''去掉應(yīng)該就可以了.以下為驗(yàn)證實(shí)例
In [10]: path2 = path.replace(’'’, ’’) In [11]: path2Out[11]: ’C:UsersxxxxxDesktopfilename with space.txt’ In [12]: os.path.exists(path2)Out[12]: True
當(dāng)前讀取手機(jī)存儲(chǔ)空間的文件時(shí),當(dāng)手機(jī)root目錄中存在還中文或帶空格的文件/文件夾時(shí)(如下圖),就會(huì)出錯(cuò).
一般這時(shí)為了要讀出這些文件夾,一般的操作為:
In [23]: cmd = ’adb shell ls /sdcard/’ In [24]: file_list = os.popen(cmd).readlines()---------------------------------------------------------------------------UnicodeDecodeError Traceback (most recent call last)<ipython-input-24-b7ae01065f81> in <module>----> 1 file_list = os.popen(cmd).readlines() UnicodeDecodeError: ’gbk’ codec can’t decode byte 0xae in position 10: illegal multibyte sequence
一般會(huì)報(bào)以上的錯(cuò)誤或是不報(bào)錯(cuò),但是中文文件/文件名可能為亂碼,從以下的help(os.popen)可以了解后,os.popen()也是不能設(shè)置encode方式的,無(wú)解哈.
In [25]: help(os.open)Help on built-in function open in module nt: open(path, flags, mode=511, *, dir_fd=None) Open a file for low level IO. Returns a file descriptor (integer). If dir_fd is not None, it should be a file descriptor open to a directory, and path should be relative; path will then be relative to that directory. dir_fd may not be implemented on your platform. If it is unavailable, using it will raise a NotImplementedError.
所以又回到之前寫(xiě)的一篇文章上,要用subprocess.run()全面替換掉os.system/os.popen,這樣就可以解決這些問(wèn)題了.
In [27]: cmd = ’adb shell ls /sdcard/’ In [28]: file_list = subprocess.run(cmd, capture_output=True, encoding=’utf-8’, shell=True).stdout. ...: splitlines() In [29]: file_list[0:3]Out[29]: [’0000’, ’00新文件夾’, ’00新文件夾 test’]
故上兩個(gè)困擾了很久的問(wèn)題,終于找到了解決方案,開(kāi)心一下
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. python 實(shí)現(xiàn)圍棋游戲(純tkinter gui)2. Python TestSuite生成測(cè)試報(bào)告過(guò)程解析3. python之cur.fetchall與cur.fetchone提取數(shù)據(jù)并統(tǒng)計(jì)處理操作4. JSP之表單提交get和post的區(qū)別詳解及實(shí)例5. python讓函數(shù)不返回結(jié)果的方法6. PHP循環(huán)與分支知識(shí)點(diǎn)梳理7. 解決AJAX返回狀態(tài)200沒(méi)有調(diào)用success的問(wèn)題8. chat.asp聊天程序的編寫(xiě)方法9. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案10. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器
