描述
方法fchdir()將當前工作目錄更改為文件描述符所代表的目錄fd.描述符必須指向打開的目錄,而不是打開的文件。
用法
以下是語法fchdir()方法 -
os.fchdir(fd)
參數
fd- 這是目錄描述符。
返回值
此方法不返回任何值。
示例
下麵的例子展示了 fchdir() 方法的用法。
#!/usr/bin/python3
import os, sys
# First go to the "/var/www/html" directory
os.chdir("/var/www/html" )
# Print current working directory
print ("Current working dir:%s" % os.getcwd())
# Now open a directory "/tmp"
fd = os.open( "/tmp", os.O_RDONLY )
# Use os.fchdir() method to change the dir
os.fchdir(fd)
# Print current working directory
print ("Current working dir:%s" % os.getcwd())
# Close opened directory.
os.close( fd )
結果
當我們運行上述程序時,它會產生以下結果 -
Current working dir:/var/www/html Current working dir:/tmp
相關用法
- Python 3 os.fchmod()用法及代碼示例
- Python 3 os.fchown()用法及代碼示例
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 os.fdopen()用法及代碼示例
- Python 3 os.fdatasync()用法及代碼示例
- Python 3 os.fsync()用法及代碼示例
- Python 3 os.fpathconf()用法及代碼示例
- Python 3 os.fstat()用法及代碼示例
- Python 3 os.ftruncate()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- Python 3 os.close()用法及代碼示例
- Python 3 os.unlink()用法及代碼示例
- Python 3 os.major()用法及代碼示例
- Python 3 os.rmdir()用法及代碼示例
- Python 3 os.isatty()用法及代碼示例
- Python 3 os.rename()用法及代碼示例
- Python 3 os.walk()用法及代碼示例
- Python 3 os.renames()用法及代碼示例
- Python 3 os.makedirs()用法及代碼示例
- Python 3 os.utime()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - os.fchdir() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。