为了账号安全,请及时绑定邮箱和手机立即绑定

python 判断文件是文件还是文件夹

python 判断文件是文件还是文件夹

qq_笑_17 2019-06-07 13:06:24
python 判断文件是文件还是文件夹
查看完整描述

3 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

在os.path模块中有个isfile的方法,该方法可以判断是不是文件,返回True说明是文件,返回False则不是文件,下面的英文是摘自python文档

os.path.isfile(path)

Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.

用法也很简单

1

2

3

import os

filename='/tmp/test.txt'

print os.path.isfile(filename)


 


查看完整回答
反对 回复 2019-06-08
?
慕侠2389804

TA贡献1719条经验 获得超6个赞

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

>>> import os.path

>>> help(os.path.isfile)

Help on function isfile in module genericpath:

 

isfile(path)

    Test whether a path is a regular file

 

>>> help(os.path.isdir)

Help on function isdir in module genericpath:

 

isdir(s)

    Return true if the pathname refers to an existing directory.

 

>>> os.path.isfile('/etc/hostname')

True

>>> os.path.isdir('/etc/hostname')

False

>>> 

两个函数都在os.path模块里面。参见上文。

 


查看完整回答
反对 回复 2019-06-08
  • 3 回答
  • 0 关注
  • 4477 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信