您的位置:首页 > 编程语言 > Python开发

python os.path模块(os.listdir和os._exit())

2017-11-22 17:00 696 查看

1.python os

fileList = os.listdir(path)
if len(fileList) == 0:
print 'The folder is empty'
os._exit()  #直接退出 Python程序,其后的代码也不会继续执行。

for file in fileList:
if not os.path.isfile(path +'\\'+ file):
print '文件不存在'
else:
print os.path.join(path+'\\',file)


更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.html


python os.path模块常用方法详解http://www.cnblogs.com/wuxie1989/p/5623435.html

2.Python os._exit() sys.exit() exit()区别

Python退出程序的方式有两种:os._exit(),
sys.exit()

1)os._exit() 直接退出 Python程序,其后的代码也不会继续执行。
2)sys.exit() 引发一个 SystemExit异常,若没有捕获这个异常,Python解释器会直接退出;捕获这个异常可以做一些额外的清理工作。0为正常退出,其他数值(1-127)为不正常,可抛异常事件供捕获。

exit() 跟 C 语言等其他语言的 exit() 应该是一样的。

os._exit() 调用 C 语言的 _exit() 函数。
__builtin__.exit 是一个 Quitter 对象,这个对象的 __call__ 方法会抛出一个 SystemExit 异常。

一般来说os._exit() 用于在线程中退出
sys.exit() 用于在主线程中退出。

参考链接:http://www.cnblogs.com/gaott/archive/2013/04/12/3016355.html

3.python 的 与或非

#! /usr/bin/env python
#coding=utf-8
# save file: logical_operators_example.py

a = True
b = False

not(a and b)
print ('(a and b) = ', (a and b))
print ('(a or b) = ', (a or b))
print ('not(a and b) = ', not(a and b))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: