• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python os.path.exists()函数总是返回false的解决方案

python 搞代码 4年前 (2022-01-07) 13次浏览 已收录 0个评论

这篇文章主要介绍了Python os.path.exists()函数总是返回false的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下面所示,如果我们用file的readline或readlines,在每一行后面都有一个\n回车符

直接os.path.exists(readline)时总会返回false

 >>> from os.path import exists >>> exists('dog.png-600') True >>> exists('dog.png-600\n') 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:\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多了一层””字符串,故将多余的””去掉应该就可以了.以下为验证实例

 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

当前读取手机存储空间的文件时,当手机root目录中存在还中文或带空格的文件/文件夹时(如下图),就会出错.

一般这时为了要读出这些文件夹,一般的操作为:

 In [23]: cmd = 'adb shell ls /sdcard/' In [24]: file_list = os.popen(cmd).readlines() --------------------------------------------------------------------------- UnicodeDecodeError            Traceback (most recent call last)  in  ----> 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,这样就可以解决这些问题了.

 <i style="color:transparent">来源gaodai$ma#com搞$$代**码)网</i>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']

故上两个困扰了很久的问题,终于找到了解决方案,开心一下

以上为个人经验,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网。如有错误或未考虑完全的地方,望不吝赐教。

以上就是Python os.path.exists()函数总是返回false的解决方案的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python os.path.exists()函数总是返回false的解决方案

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址