Python方法fchdir()将当前工作目录更改为文件描述符fd表示的目录。描述符必须引用打开的目录,而不是打开的文件。
os.fchdir(fd);
fd - 这是目录描述符。
以下示例显示了fchdir()方法的用法。
#!/usr/bin/python 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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
HarmonyOS快速入门与实战 -〔QCon+案例研习社〕