色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Python os.path.exists()函數總是返回false的解決方案

瀏覽:73日期:2022-06-24 17:39:27

如下面所示,如果我們用file的readline或readlines,在每一行后面都有一個n回車符

直接os.path.exists(readline)時總會返回false

>>> from os.path import exists>>> exists(’dog.png’)True>>> exists(’dog.pngn’)False

使用item.strip(’n’) #前面的item為我定義的變量

去掉后再傳遞給os.path.exists(item) 就OK了。

補充:當os.path.exists(path)的path中包含有空格時返回結果為False的解決方案

之前有個問題一直沒有解決, 當路徑中或文件名中存在空格時,用os.path.exists(path)判斷是否存在時,都會返回False. 百思不得其解. 今天在用ipython偶到想到想了解一下到底是什么原因?

事實上,當用input()接收path輸入時,path中有空格時,生成的str是不一樣的. 如下:

In [4]: path = input(’請將文件拖入:’)

請將文件拖入:'C:UsersxxxxxDesktopfilename with space.txt'

In [5]: pathOut[5]: ’'C:UsersxxxxxDesktopfilename with space.txt'’ In [6]: path1 = input(’請將文件拖入:’)

請將文件拖入: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時生了的str多了一層''字符串,故將多余的''去掉應該就可以了.以下為驗證實例

In [10]: path2 = path.replace(’'’, ’’) In [11]: path2Out[11]: ’C:UsersxxxxxDesktopfilename with space.txt’ In [12]: os.path.exists(path2)Out[12]: True

當前讀取手機存儲空間的文件時,當手機root目錄中存在還中文或帶空格的文件/文件夾時(如下圖),就會出錯.

Python os.path.exists()函數總是返回false的解決方案

一般這時為了要讀出這些文件夾,一般的操作為:

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()也是不能設置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’]

故上兩個困擾了很久的問題,終于找到了解決方案,開心一下

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产欧美va欧美va香蕉在线观 | 国产五区 | 中字毛片 | 久久精品免费观看 | 国产一区日韩二区欧美三 | 日本毛片在线 | 欧美亚洲第一区 | 亚洲欧美久久一区二区 | 毛片视频在线免费观看 | 国产精品色综合久久 | 国产精品国产三级国产在线观看 | 久草免费网站 | 视频二区国产 | 亚洲国产精品成人午夜在线观看 | 一区二区三区免费视频 www | 真实国产乱子伦高清 | 女人张开腿让男人添 | 在线亚洲一区二区 | 亚洲三级视频 | 欧美日本色 | 日本伊人精品一区二区三区 | 国产永久高清免费动作片www | 日韩精品永久免费播放平台 | 免费在线黄色网址 | 96精品免费视频大全 | gogo999亚洲肉体艺术大胆 | 国产免费观看a大片的网站 国产免费黄色网址 | 亚洲欧美国产精品 | 亚洲国产毛片aaaaa无费看 | 亚洲国产片在线观看 | 日本欧美一级二级三级不卡 | 视频一区久久 | 香港经典毛片a免费观看 | 国产精品久久久久亚洲 | 亚洲第一在线 | 怡红院成人永久免费看 | 免费观看久久 | 日韩a一级欧美一级 | 俄罗斯小屁孩cao大人免费 | 狠狠色噜噜狠狠狠米奇9999 | 欧美日韩亚洲综合久久久 |