python 操做windows下目录的相关函数

一、os.getcwd()         os.chdir(dir)      #获取当前目录 ,改变当前目录为dirwindows

二、os.listdir(dir)        #获取目录内容,其结果为list类型spa

三、os.mkdir(dir)        #建立目录pdo

四、os.rmdir(dir)         # 删除空目录,若是目录中有内容,则出错rem

五、os.path.isdir(dir)                    #判断是否为目录get

六、os.path.isfile(file)                   #判断是否为文件cmd

七、os.path.isabs(path)               #判断是否为绝对路径it

八、os.path.abspath(path)           #取得绝对路径扩展

九、os.path.dirname(path)           #取得父目录file

十、os.path.exists(path)            #判断目录或文件是否存在遍历

十一、os.path.getsize(path)          #取得文件大小

十二、os.path.getctime(path)   getmtime(path) getatime()      #取得文件的建立、修改、最后存储的时间

#不过取得是浮点数,须要用time模块中的time.ctime(float) 或time.localtime(float)转换成可识别的格式

1三、os.path.split(path)             #分割路径,结果为元组,如(‘c:\\windows','system32')

1四、os.path.splitext(path)        #分割扩展名,结果如 (‘c:\\windows\\system32\\cmd','.exe')

1五、os.rename(file1,file2)         #将file1文件改名为file2文件

1六、os.remove(file)                  #删除file文件

1七、os.walk(path,topdown) #目录遍历

一下是调用os.walk的例子,遍历指定的目录

def walk_dir(dir, topdown=True):
    for root, dirs, files in os.walk(dir, topdown):
        for name in files:
            print(os.path.join(root, name))
        for name in dirs:
            print(os.path.join(root, name))

 

glob模块

glob.glob("*.py")                    #返回当前目录下全部以.py为后缀的目录或文件

 

shutil模块

shutil.copyfile(src,dst)             #拷贝文件

shutil.copystat(src,dst)           #拷贝文件,连同文件的stat一块儿拷贝

shutil.copytree(src,dst)           #拷贝目录,拷贝以前dst必须不存在

相关文章
相关标签/搜索