Echec du contrôle os.path.isdir

J’ai le code suivant où j’essaie de vérifier le répertoire “Gerrits / HEAD / wlan” et puis faire des opérations, pour une raison quelconque si la condition pour vérifier le répertoire continue à échouer même si le répertoire existe? rien de mal avec la condition if @ if (os.path.isdir (SCRIPT_ROOT + “/ Gerrits / HEAD / wlan”)): ci-dessous

import os import subprocess from subprocess import check_call SCRIPT_ROOT=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0] print SCRIPT_ROOT def main (): if (os.path.isdir(SCRIPT_ROOT + "/Gerrits/HEAD/wlan")): print "SCRIPT_ROOT/Gerrits/HEAD/wlan already exists,cloning it again to the tip" check_call("rm -rf $SCRIPT_ROOT/Gerrits/HEAD/wlan ", shell=True) check_call("cd Gerrits/HEAD",shell=True) else: print "SCRIPT_ROOT/Gerrits/HEAD/wlan doesn't exist,cloning it" os.makedirs("Gerrits/HEAD/wlan") check_call("cd Gerrits/HEAD",shell=True) currdir=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0] if __name__ == '__main__': main() 

Erreur:-

 SCRIPT_ROOT/Gerrits/HEAD/wlan doesn't exist,cloning it Traceback (most recent call last): File "test.py", line 21, in  main() File "test.py", line 16, in main os.makedirs("Gerrits/HEAD/wlan") File "/usr/lib/python2.6/os.py", line 157, in makedirs mkdir(name, mode) OSError: [Errno 17] File exists: 'Gerrits/HEAD/wlan' 

Ajoutez .ssortingp() à vos appels communicate()[0] , le code tel .ssortingp() inclut le retour à la ligne finale dans la sortie.

Juste pour être sûr, votre script que je viens de tester sur un box Linux avec Python 2.5.

 import os import subprocess from subprocess import check_call SCRIPT_ROOT=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0].ssortingp() print SCRIPT_ROOT def main (): if (os.path.isdir(SCRIPT_ROOT + "/Gerrits/HEAD/wlan")): print "SCRIPT_ROOT/Gerrits/HEAD/wlan already exists,cloning it again to the tip" check_call("rm -rf %s/Gerrits/HEAD/wlan" % SCRIPT_ROOT, shell=True) check_call("cd Gerrits/HEAD",shell=True) else: print "SCRIPT_ROOT/Gerrits/HEAD/wlan doesn't exist,cloning it" os.makedirs("Gerrits/HEAD/wlan") check_call("cd Gerrits/HEAD",shell=True) currdir=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0].ssortingp() if __name__ == '__main__': main() 

Et sa sortie:

 vlazarenko@xx:~$ python o.py /media/home/vlazarenko SCRIPT_ROOT/Gerrits/HEAD/wlan already exists,cloning it again to the tip 

Lorsque j’exécute cette ligne de code ici:

 SCRIPT_ROOT=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0] 

… la valeur de SCRIPT_ROOT a une nouvelle ligne

 >>> import os >>> import subprocess >>> ROOT = subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0] >>> ROOT '/Users/bgporter/personal\n' 

… qui fait cet appel

 if (os.path.isdir(SCRIPT_ROOT + "/Gerrits/HEAD/wlan")): 

se comporter différemment que vous ne le souhaitez. Vous pouvez soit appeler ssortingp () sur cette valeur, ou si vous voulez toujours obtenir le répertoire de travail en cours, vous pouvez le faire beaucoup plus facilement en appelant os.getcwd()

De même, vous pouvez utiliser la fonction os.removedirs() pour supprimer de manière récursive les répertoires que vous ne voulez pas au lieu de les extraire.