首页 > 编程语言 > 详细

Python 常用模块

时间:2019-06-26 17:50:08      阅读:110      评论:0      收藏:0      [点我收藏+]

os模块

提供了非常丰富的方法用来处理文件和目录。

os.getcwd()  返回当前工作目录

os.getegid()  得到有效组id  os.getgid()  得到组id

os.geteuid()  得到有效用户id  os.getuid()

os.chdir(dir)  更改当前目录  os.chdir(‘d:\\outlook‘)

os.system(cmd)  利用系统调用,运行cmd命令

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 切换到 "/var/www/html" 目录
os.chdir("/var/www/html" )

# 打印当前目录
print "当前工作目录 : %s" % os.getcwd()

# 打开 "/tmp"
fd = os.open( "/tmp", os.O_RDONLY )

# 使用 os.fchdir() 方法修改目录
os.fchdir(fd)

# 打印当前目录
print "当前工作目录 : %s" % os.getcwd()

# 关闭文件
os.close( fd )

--------------------------------
当前工作目录 : /var/www/html
当前工作目录 : /tmp
--------------------------------

- all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc.
- os.path is one of the modules posixpath, or ntpath
- os.name is ‘posix‘, ‘nt‘, ‘os2‘, ‘ce‘ or ‘riscos‘
- os.curdir is a string representing the current directory (‘.‘ or ‘:‘)
- os.pardir is a string representing the parent directory (‘..‘ or ‘::‘)
- os.sep is the (or a most common) pathname separator (‘/‘ or ‘:‘ or ‘\\‘)
- os.extsep is the extension separator (‘.‘ or ‘/‘)
- os.altsep is the alternate pathname separator (None or ‘/‘)
- os.pathsep is the component separator used in $PATH etc
- os.linesep is the line separator in text files (‘\r‘ or ‘\n‘ or ‘\r\n‘)
- os.defpath is the default search path for executables
- os.devnull is the file path of the null device (‘/dev/null‘, etc.)

sys模块

 

from...import 语句

python的from语句让你从模块中导入一个指定的部分到当前命名空间中。

 

from modname import name1[,name2[, ... nameN]]
例如:要导入模块fib的fibonacci函数,使用如下语句
from fib import fibonacci

 

from...import * 语句

 

把一个模块的所有内容完全都导入到当前的命名空间也是可行的,只需要使用如下声明:

 

from modname import *
这提供了一个简单的方法来导入一个模块中的所有项目。然而这种声明不该被过多地使用。
例如我们想一次性引入math模块中的所有东西,语句如下:
form math import *

 

Python 常用模块

原文:https://www.cnblogs.com/green777/p/11091302.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!