Python3 OS 檔案/目錄方法
概述
os.getcwd() 方法用于傳回目前工作目錄。
文法
getcwd()方法文法格式如下:
os.getcwd()
參數
- 無
傳回值
傳回目前程序的工作目錄。
執行個體
以下執行個體示範了 getcwd() 方法的使用:
#!/usr/bin/python3
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)
# 關閉檔案
os.close( fd )
執行以上程式輸出結果為:
目前工作目錄 : /var/www/html
目前工作目錄 : /tmp