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

来源:互联网 发布:it认证培训班 编辑:程序博客网 时间:2024/05/18 01:33

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.pya = Trueb = Falsenot(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))


原创粉丝点击