import os# f=r'C:\Users\Miya.Tan\PycharmProjects\python课堂\day06\test.txt'# os.remove(f)# os.rename(f,'test1.txt')#创建一个文件夹的时候,mkdir和makedirs无区别。# os.mkdir('tanailing') #创建文件夹# os.makedirs('python1')#创建目录#创建一个二层的目录。mkdir只能在存在的父目录下创建。makedirs会自动创建父目录# os.makedirs('china/shenzhen') #创建目录# os.mkdir('city/smallcity') #FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'city/smallcity' 只能在存在的文件目录下创建文件夹#删除E盘下的空文件夹# os.removedirs('china') #只能删除空文件夹 OSError: [WinError 145] 目录不是空的。: 'china'# print(os.listdir(r'C:\Users\Miya.Tan\PycharmProjects\python课堂\day06')) #显示该目录下面的所有文件和文件夹,不传就表示当前文件夹.但是只是当前直接子目录,不下渗# print(os.path.isdir(r'C:\Users\Miya.Tan\PycharmProjects\python课堂\day06'))#判断是否为文件夹 bool# print(os.path.isfile(r'C:\Users\Miya.Tan\PycharmProjects\python课堂\day06')) #判断是否为文件 bool# print(os.path.exists('china')) #判断文件或者文件夹是否存在# res =os.walk(r'C:\Users\Miya.Tan\PycharmProjects\python课堂')## for path,dir_name,filename in res:# print(path)# print(dir_name)# print(filename)# os.mkdir(r'C:\Users\Miya.Tan\PycharmProjects\python课堂/test1')res = os.listdir(r'C:\Users\Miya.Tan\PycharmProjects\python课堂')print(res)# for one in res:# path = os.path.join(r'C:\Users\Miya.Tan\PycharmProjects\python课堂\dirlist测试',one)# # print(path)# # print(os.path.isdir(path))# if os.path.isdir(path):# os.removedirs(path)for file in res: print(file) pathroot=r'C:\Users\Miya.Tan\PycharmProjects\python课堂' path = os.path.join(pathroot,file) if os.path.isdir(path): if os.listdir(path) ==[]: os.removedirs(path) elif os.path.isfile(path): if os.path.getsize(path) == 0: os.remove(path)