概述
os.getcwd() 方法用于返回当前工作目录。
语法
getcwd() 方法语法格式如下:
os
.
getcwd
()
参数
- 无
返回值
返回当前进程的工作目录。
实例
以下实例演示了 getcwd() 方法的使用:
1
#
!/usr/bin/python
2
#
-*- coding: UTF-8 -*-
3
4
import
os, sys
5
6
#
切换到 "/var/www/html" 目录
7
os.chdir(
"
/igihub/ipython/base/file_handle
"
)
8
9
#
打印当前目录
10
print
(
"
当前工作目录 : %s
"
%
os.getcwd())
11
12
#
打开 "/tmp"
13
fd = os.open(
"
/ipython/base
"
, os.O_RDONLY )
14
15
#
使用 os.fchdir() 方法修改目录
16
os.fchdir(fd)
17
18
#
打印当前目录
19
print
(
"
当前工作目录 : %s
"
%
os.getcwd())
20
21
#
关闭文件
22
os.close( fd )
用的时候,记得把路径换成你所需要的就行了。
参考:
1 https://www.runoob.com/python/os-getcwd.html

