os.getcwdb()

本页内容
上一节: Python3_os_getcwd 下一节: Python3_os_isatty

Python3 os.getcwdb() 方法

Python3 OS 文件/目录方法

概述

os.getcwdb() 方法用于返回一个字节串(bytestring),表示当前工作目录。

ByteString 是一个可以对应所有可能的字节序列的 UTF-8 字符串。

该方法在 Python3.8 版中进行了更改:该函数在 Windows 上使用 UTF-8 编码,而不是 ANSI。

语法

getcwdb()方法语法格式如下:


示例

os.getcwdb()

参数

返回值

返回一个当前工作目录的 Unicode 对象。

==

以下实例演示了 getcwdb() 方法的使用:


示例

#!/usr/bin/python3

import os, sys

# 切换到 "/var/www/html" 目录

os.chdir("/var/www/html" )

# 打印当前目录

print ("当前工作目录 : %s" % os.getcwdb())

# 打开 "/tmp"

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

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

os.fchdir(fd)

# 打印当前目录

print ("当前工作目录 : %s" % os.getcwdb())

# 关闭文件

os.close( fd )

执行以上程序输出结果为:


示例

当前工作目录 : b'/var/www/html'
当前工作目录 : b'/private/tmp'

Python3 OS 文件/目录方法

上一节: Python3_os_getcwd 下一节: Python3_os_isatty
此页面最后编辑于2022年8月17日 (星期三) 21:09。