文件操作

判断是文件还是文件夹

1
2
3
4
5
6
7
import os
if os.path.isdir(path):
print "it's a directory"
elif os.path.isfile(path):
print "it's a normal file"
else:
print "it's a special file(socket,FIFO,device file)"

判断文件是否存在

1
2
3
4
5
6
import os

>>> os.path.exists('d:/assist')
True
>>> os.path.exists('d:/assist/getTeacherList.py')
True

获取文件大小

1
2
3
import os

os.path.getsize(path)