Python Module Learning

Source: Internet
Author: User

System-related Information module: Import sys
sys.argv 是一个 list,包含所有的命令行参数.    sys.stdout sys.stdin sys.stderr 分别表示标准输入输出,错误输出的文件对象.    sys.stdin.readline() 从标准输入读一行 sys.stdout.write("a") 屏幕输出a    sys.exit(exit_code) 退出程序    sys.modules 是一个dictionary,表示系统中所有可用的module    sys.platform 得到运行的操作系统环境    sys.path 是一个list,指明所有查找module,package的路径.  
Operating system-related calls and actions: Import OS
os.environ 一个dictionary 包含环境变量的映射关系   os.environ["HOME"] 可以得到环境变量HOME的值     os.chdir(dir) 改变当前目录 os.chdir(‘d:\\outlook‘)   注意windows下用到转义     os.getcwd() 得到当前目录     os.getegid() 得到有效组id os.getgid() 得到组id     os.getuid() 得到用户id os.geteuid() 得到有效用户id     os.setegid os.setegid() os.seteuid() os.setuid()     os.getgruops() 得到用户组名称列表     os.getlogin() 得到用户登录名称     os.getenv 得到环境变量     os.putenv 设置环境变量     os.umask 设置umask     os.system(cmd) 利用系统调用,运行cmd命令
Os.path Module
os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录,返回bool值os.path.exists()函数用来检验给出的路径是否真地存在 返回boolos.path.getsize(name):获得文件大小,如果name是目录返回0L 返回long 单位是字节os.path.abspath(name):获得绝对路径os.path.normpath(path):规范path字符串形式, 结果一般情况下把/变为//,os.path.split(name):将name分割成路径名和文件名,结果为(路径名,文件名.文件扩展名)(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)os.path.splitext(filename):分离文件名与扩展名 结果为(filename,扩展名) 如果参数为一个路径 则返回(路径,‘‘)os.path.join(path,name): 连接目录与文件名或目录 结果为path/nameos.path.basename(path):返回文件名 实际为把path的最后一个"/"分割,返回后者。不管参数是一个路径还是文件 与os.path.split(name)相同 不同之处后者返回两个值得元组os.path.dirname(path): 返回文件路径 实际为把path的最后一个"/"分割,返回前者。不管参数是一个路径还是文件os.system()函数用来运行shell命令
Built-in modules (can be used directly without import) commonly used built-in functions:
Help (obj) online, obj is any type callable (obj) to see if an obj can call repr (obj) like a function to get the representation string of obj, which can be used to reconstruct a copy of the object Eval_r ( STR) represents a valid Python expression that returns the expression dir (obj) to view the name hasattr visible in obj's name space (Obj,name) to see if there is a name in the name space of obj Getatt R (Obj,name) obtains a name SetAttr (Obj,name,value) in the name space of obj as a name in the name space of obj pointing to Vale, the object delattr (obj , name) removes a name VARs (obj) from the name space of obj to return an object name space. Use dictionary to indicate that locals () returns a local name space, using dictionary to represent globals () to return a global name space, using the dictionary representation of type (obj) to view an obj Type isinstance (obj,cls) View if obj is not CLS instance Issubclass (SUBCLS,SUPCLS) View Subcls is Supcls subclass ################## type Convert ################# #chr (i) put an ASCII value into the character ord (i) converts a character or Unicode character into an ASCII value Oct (x) converts an integer x into an octal representation of the string hex (x) to the integer x A string that is hexadecimal represented by str (obj) gets the string description of obj list (seq) converts a sequence into a list tuple (seq) to convert a sequence into a tuple dict (), Dict ( list) into a dictionary int (x) converted to an integer long (x) into a long interger fLoat (x) is converted to a floating-point number Complex (x) converted to complex Max (...) to maximize min (...) to find minimum  

Too many modules, it's better to watch more official documents ·

Python Module Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.