Latest web development tutorials

méthode Python os.chdir ()

méthode Python Fichier (File) méthodes fichier / répertoire Python OS


contour

méthode os.chdir () est utilisée pour changer le répertoire courant dans le chemin spécifié.

grammaire

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

os.chdir(path)

Paramètres

  • chemin - pour basculer vers le nouveau chemin.

Valeur de retour

Si vous autorisez l'accès à retourner Vrai, sinon False.

Exemples

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

path = "/tmp"

# 查看当前工作目录
retval = os.getcwd()
print "当前工作目录为 %s" % retval

# 修改当前工作目录
os.chdir( path )

# 查看修改后的工作目录
retval = os.getcwd()

print "目录修改成功 %s" % retval

La sortie du programme ci-dessus est:

当前工作目录为 /www
目录修改成功 /tmp

méthode Python Fichier (File) méthodes fichier / répertoire Python OS