如何查找Python中是否存在目录
在Python的os模块中,有没有办法find一个目录是否存在,如: 
 >>> os.direxists(os.path.join(os.getcwd()), 'new_folder')) # in pseudocode True/False 
	
 你正在寻找os.path.isdir ,或者os.path.exists如果你不在乎它是一个文件还是一个目录。 
例:
 import os print(os.path.isdir("/home/el")) print(os.path.exists("/home/el/myfile.txt")) 
 很近! 如果您传入当前存在的目录名称, os.path.isdir将返回True 。 如果它不存在或不是目录,则返回False 。 
 是的,使用os.path.exists() 。 
是的使用os.path.isdir(path)
如:
 In [3]: os.path.exists('/d/temp') Out[3]: True 
 可能折腾在os.path.isdir(...)是肯定的。 
  Python 3.4将pathlib模块引入标准库,提供了一种面向对象的方法来处理文件系统path: 
 In [1]: from pathlib import Path In [2]: p = Path('/usr') In [3]: p.exists() Out[3]: True In [4]: p.is_dir() Out[4]: True In [5]: q = p / 'bin' / 'vim' In [6]: q.exists() Out[6]: True In [7]: q.is_dir() Out[7]: False 
Pathlib也可以通过PyPi上的pathlib2模块在 Python 2.7 上使用。
操作系统为您提供了很多这些function:
 import os os.path.isdir(dir_in) #True/False: check if this is a directory os.listdir(dir_in) #gets you a list of all files and directories under dir_in 
如果inputpath无效,listdir将会抛出exception。
 只要提供os.stat版本(python 2): 
 import os, stat, errno def CheckIsDir(directory): try: return stat.S_ISDIR(os.stat(directory).st_mode) except OSError, e: if e.errno == errno.ENOENT: return False raise 
 #You can also check it get help for you if not os.path.isdir('mydir'): print('new directry has been created') os.system('mkdir mydir')