Python os.path.exists()函數(shù)總是返回false的解決方案
如下面所示,如果我們用file的readline或readlines,在每一行后面都有一個\n回車符
直接os.path.exists(readline)時總會返回false
>>> from os.path import exists
>>> exists('dog.png')
True
>>> exists('dog.png\n')
False
使用item.strip('\n') #前面的item為我定義的變量
去掉后再傳遞給os.path.exists(item) 就OK了。
補充:當(dāng)os.path.exists(path)的path中包含有空格時返回結(jié)果為False的解決方案
之前有個問題一直沒有解決, 當(dāng)路徑中或文件名中存在空格時,用os.path.exists(path)判斷是否存在時,都會返回False. 百思不得其解. 今天在用ipython偶到想到想了解一下到底是什么原因?
事實上,當(dāng)用input()接收path輸入時,path中有空格時,生成的str是不一樣的. 如下:
In [4]: path = input('請將文件拖入:')
請將文件拖入:"C:\Users\xxxxx\Desktop\filename with space.txt"
In [5]: path
Out[5]: '"C:\\Users\\xxxxx\\Desktop\\filename with space.txt"'
In [6]: path1 = input('請將文件拖入:')
請將文件拖入:C:\Users\xxxxx\Desktop\filenamewithspace.txt
In [7]: path1 Out[7]: 'C:\\Users\\xxxxx\\Desktop\\filenamewithspace.txt' In [8]: os.path.exists(path) Out[8]: False In [9]: os.path.exists(path1) Out[9]: True
很明顯,帶有space時生了的str多了一層""字符串,故將多余的""去掉應(yīng)該就可以了.以下為驗證實例
In [10]: path2 = path.replace('\"', '')
In [11]: path2
Out[11]: 'C:\\Users\\xxxxx\\Desktop\\filename with space.txt'
In [12]: os.path.exists(path2)
Out[12]: True
當(dāng)前讀取手機存儲空間的文件時,當(dāng)手機root目錄中存在還中文或帶空格的文件/文件夾時(如下圖),就會出錯.

一般這時為了要讀出這些文件夾,一般的操作為:
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
一般會報以上的錯誤或是不報錯,但是中文文件/文件名可能為亂碼,從以下的help(os.popen)可以了解后,os.popen()也是不能設(shè)置encode方式的,無解哈.
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.
所以又回到之前寫的一篇文章上,要用subprocess.run()全面替換掉os.system/os.popen,這樣就可以解決這些問題了.
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']
故上兩個困擾了很久的問題,終于找到了解決方案,開心一下
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
淺談PyTorch的數(shù)據(jù)讀取機制Dataloader與Dataset
這篇文章主要介紹了淺談PyTorch的數(shù)據(jù)讀取機制Dataloader與Dataset,DataLoader的作用是構(gòu)建一個可迭代的數(shù)據(jù)裝載器,每次執(zhí)行循環(huán)的時候,就從中讀取一批Batchsize大小的樣本進行訓(xùn)練,需要的朋友可以參考下2023-07-07
在tensorflow中設(shè)置保存checkpoint的最大數(shù)量實例
今天小編就為大家分享一篇在tensorflow中設(shè)置保存checkpoint的最大數(shù)量實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python數(shù)學(xué)形態(tài)學(xué)實例分析
這篇文章主要介紹了Python數(shù)學(xué)形態(tài)學(xué),結(jié)合實例形式分析了Python數(shù)學(xué)運算與圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
pyside6-uic生成py代碼中文為unicode(亂碼)的問題解決方案
這篇文章主要介紹了如何解決pyside6-uic生成py代碼中文為unicode(亂碼)的問題,文中通過代碼和圖文介紹的非常詳細,對大家解決問題有一定的幫助,需要的朋友可以參考下2024-02-02
python如何使用雙線性插值計算網(wǎng)格內(nèi)數(shù)據(jù)
這篇文章主要介紹了python如何使用雙線性插值計算網(wǎng)格內(nèi)數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
python利用logging模塊實現(xiàn)根據(jù)日志級別打印不同顏色日志的代碼案例
這篇文章主要介紹了python利用logging模塊實現(xiàn)根據(jù)日志級別打印不同顏色日志,本文通過實例代碼給大家詳細講解,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12

