python学习笔记(1)--os和os.path模块

1.os.path模块

os.path. abspath ( path )

Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to calling the function normpath()  as follows: normpath(join(os.getcwd(),path)).

os.path. dirname ( path )

Return the directory name of pathname path. This is the first element of the pair returned by passing path to the function split().

Example:

#/usr/bin/python

import os.path

def main():
    localdir = os.path.abspath(os.path.dirname(__name__))
    print localdir


if __name__ == '__main__':
    main()

Result:

/home/ye/python


os.path. join ( path1 [, path2 [, ... ] ] )

Join one or more path components intelligently. If any component is an absolute path, all previous components (on Windows, including the previous drive letter,if there was one) are thrown away, and joining continues. The return value is the concatenation of path1, and optionally path2, etc., with exactly one directory separator (os.sep) following each non-empty part except the last.(This means that an empty last part will result in a path that ends with a separator.) Note that on Windows, since there is a current directory for each drive,os.path.join("c:","foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

Example:

#/usr/bin/python

import os.path

def main():
    localdir = os.path.abspath(os.path.dirname(__name__))
    KEY = os.path.join(localdir,'server.key')
    print KEY 


if __name__ == '__main__':
    main()

Result:

/home/ye/python/server.key


os.path. expanduser ( path )

On Unix and Windows, return the argument with an initial component of ~ or~user replaced by that user‘s home directory.

On Unix, an initial ~ is replaced by the environment variable HOME if it is set; otherwise the current user’s home directory is looked up in the password directory through the built-in module pwd. An initial~useris looked up directly in the password directory.

On Windows, HOME andUSERPROFILE will be used if set,otherwise a combination of HOMEPATH and HOMEDRIVE will be used. An initial~user is handled by stripping the last directory component from the created user path derived above.

If the expansion fails or if the path does not begin with a tilde, the path is returned unchanged.

os.path. exists ( path )

Return True if path refers to an existing path. ReturnsFalse for broken symbolic links. On some platforms, this function may return False if permission is not granted to executeos.stat() on the requested file, even if thepath physically exists.


2.os模块

os. environ

A mapping object representing the string environment. For example,environ['HOME'] is the pathname of your home directory (on some platforms),and is equivalent to getenv("HOME") in C.

This mapping is captured the first time the os module is imported,typically during Python startup as part of processingsite.py. Changes to the environment made after this time are not reflected inos.environ,except for changes made by modifying os.environ directly.

If the platform supports the putenv() function, this mapping may be used to modify the environment as well as query the environment.putenv() will be called automatically when the mapping is modified.

Note:

Calling putenv() directly does not change os.environ, so it’s better to modify os.environ.

Note:

On some platforms, including FreeBSD and Mac OS X, setting environ may cause memory leaks. Refer to the system documentation for putenv().

If putenv() is not provided, a modified copy of this mapping may be passed to the appropriate process-creation functions to cause child processes to use a modified environment.

If the platform supports the unsetenv() function, you can delete items in this mapping to unset environment variables.unsetenv() will be called automatically when an item is deleted fromos.environ, and when one of thepop() or clear() methods is called.

Changed in version 2.6: Also unset environment variables when calling os.environ.clear()and os.environ.pop().

            



Example:

>>> import os
>>> print "Environment is",os.environ
Environment is {'XAUTHORITY': '/home/ye/.Xauthority',......}




os. getcwd ( )

获取当前路径

Example:

>>> import os
>>> print "current directory is",os.getcwd()
current directory is /home/ye/python





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值