无涯教程-OS File - os.getcwd()函数

93 阅读1分钟

Python方法getcwd()返回进程的当前工作目录。

os.getcwd() - 语法

cwd=os.getcwd()

os.getcwd() - 返回值

此方法返回进程的当前工作目录。

os.getcwd() - 示例

以下示例显示getcwd()方法的用法。

#!/usr/bin/python

import os, sys

# 首先转到“/var/www/html”目录 os.chdir("/var/www/html" )

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

# 现在打开一个目录“/tmp” fd=os.open( "/tmp", os.O_RDONLY )

# 使用OS.FCHDIR()方法更改DIR os.fchdir(fd)

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

# 关闭已打开的目录。 os.close( fd )

当无涯教程运行上面的程序时,它产生以下输出-

Current working dir : /var/www/html
Current working dir : /tmp

参考链接

www.learnfk.com/python/os-g…