2020-04-16
# 获取当前目录的三种方法 os.getcwd() os.path.abspath('.') os.path.abspath(os.curdir) path = os.getcwd() #os.listdir(path) 获取路径下的文件与名录名称 #os.path.isdir(filepath) 判断对象是否是文件夹;filepath 必须是完整路径,可以找到该文件 #os.walk(path) #返回当前dirpath文件路径,dirnames 所有文件目录名称,filenames 所有文件名称
#os.walk(): 先返回当前路径下的path,dir,file;然后再逐个返回子目录下的属性,递归执行;当仅仅需要第一级目录下的内容的时候,可以使用break跳出循环
for dirpath, dirnames, filenames in os.walk(path):
allfiles = [os.path.join(dirpath, file) fo file in filenames]
alldirs = [os.path.join(dirpath, dir) for dir in dirnames]