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

方法一:

 1 # -*- coding:utf-8 -*-
 2 import os
 3 import sys
 4 from uiautomator import device as d
 5 
 6 filepath = rE:\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
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3