【Python】os.path.isfile()的使用方法汇总

方法一:

 1 # -*- coding:utf-8 -*-
 2 import os
 3 import sys
 4 from uiautomator import device as d
 5
 6 filepath = r‘E:\Project\A3A_8_4G\exercise\app_list\hello.apk‘
 7
 8 if os.path.isfile(filepath):
 9     print "true"
10 else:
11     print "false"

总结:如果变量filepath中给出的是一个绝对路径的话,那么在使用os.path.isfile()时就只需要将这个绝对路径对应的变量传进来就行

方法二:

 1 # -*- coding:utf-8 -*-
 2 import os
 3 import sys
 4 from uiautomator import device as d
 5
 6 filepath = sys.path[0] + "\\app_list\\"
 7 filename = "hello.apk"
 8
 9 if os.path.isfile(os.path.join(filepath,filename)):
10     print "true"
11 else:
12     print "false"

总结:如果将路径和文件分开来写的话,那么在使用os.path.isfile()时,则必须将这两个连接起来使用,使用os.path.join()方法将路径和文件连接起来

方法三:

1 # -*- coding:utf-8 -*-
2 import os
3 import sys
4 from uiautomator import device as d
5
6 if os.path.isfile(‘E:\\Project\\A3A_8_4G\\exercise\\app_list\\hello.apk‘):
7     print "true"
8 else:
9     print "false"

总结:这个就是直接在os.path.isfile()中传入绝对路径,但需要注意的是需要将\号进行转义,不然也会报错了

原文地址:https://www.cnblogs.com/51study/p/9335779.html

时间: 2024-08-28 05:06:55

【Python】os.path.isfile()的使用方法汇总的相关文章

python中由于中文路径引起的os.path.isfile(imgpath) == False问题

昨天在用python脚本处理文件的时候,遇到了题述问题,明明文件时存在的,但是在用os.path.isfile(imgpath) == False进行判断的时候总是成立,在一开始以为是正反斜杠windows与linux不同导致的,后来发现时因为中文路径造成的. 在网上查阅了解决办法如下: imgpath = unicode(imgpath, "utf8") 利用上述语句将imgpath的编码进行转换,然后再进行判断以及后续的图片读取(使用cv2模块)就都没有问题了.

python os.path模块--转载

os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径. os.path.dirname(path) #返回文件路径 os.path.exists(path)  #路径存在则返回True,路径损坏返回False os.path.lexists  #路径存在则返回True,路径损坏也返回True os.path.expan

[python] os.path说明

os.path - Common pathname manipulations操作 This module implements some useful functions on pathnames. To read or write files see open(), and for accessing the filesystem see the os module. The path parameters can be passed as either strings, or bytes.

python os.path模块常用方法详解

python os.path模块常用方法详解 1.   os.path.abspath(path)   返回path规范化的绝对路径. >>> import os    >>> os.path.abspath('pjc.txt')     '/home/pjc/pjc.txt' >>> os.path.abspath('c:\\test.csv')         #Windows主机指定完美的路径    'c:\\test.csv' 2.os.pat

Python——os.path模块

Python 2.7.8 该模块实现了一些关于路径名的函数. os.path.abspath(path) 返回所给参数的绝对路径.  os.path.basename(path)  Return the base name of pathname path. This is the second element of the pair returned by passing path to the function split(). Note that the result of this fu

python os.path 模块

os.path模块用法: 1, os.path.basename() >>> os.path.basename('/share/Public/cmiao')'cmiao' basename()函数并不会去判断这个路径是否存在,它只是简单的将最后一个/后面的作为文件名返回.至于是不是它不管>>> os.path.basename('/share/Public/cmiao/')'' 由于最后一个/没有东西,他就认为这个目录路径,没有文件名.所以返回了空字符串 2, os.p

python os.path模块用法详解

abspath 返回一个目录的绝对路径 Return an absolute path. >>> os.path.abspath("/etc/sysconfig/selinux") '/etc/sysconfig/selinux' >>> os.getcwd() '/root' >>> os.path.abspath("python_modu") '/root/python_modu' basename 返回一个

Python os.path

os.path.abspath(path) #返回绝对路径os.path.basename(path) #返回文件名os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径.os.path.dirname(path) #返回文件路径os.path.exists(path) #路径存在则返回True,路径损坏返回Falseos.path.lexists #路径存在则返回True,路径损坏也返回Trueos.path.expanduser(pa

python os.path模块

os.path.abspath(path) #返回绝对路径>>>print os.path.abspath("D:\\SQAP\\SQAP Training.pdf")>>>D:\SQAP\SQAP Training.pdfos.path.basename(path) #返回文件名>>>print os.path.basename("D:\\SQAP\\SQAP Training.pdf")>>>