Latest web development tutorials

méthode python3 de os.getcwd ()

Python3 OS méthodes fichier / répertoire Python3 OS méthodes fichier / répertoire


contour

méthode os.getcwd () retourne le répertoire de travail courant.

grammaire

getcwd () Syntaxe de la méthode est la suivante:

os.getcwd()

Paramètres

  • aucun

Valeur de retour

Renvoie le répertoire de travail du processus en cours.

Exemples

L'exemple suivant illustre la méthode getcwd () d'utilisation:

#!/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)

# 打印当前目录
print ("当前工作目录 : %s" % os.getcwd())

# 关闭文件
os.close( fd )

La sortie du programme ci-dessus est:

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

Python3 OS méthodes fichier / répertoire Python3 OS méthodes fichier / répertoire