Python开发--os.path

来源:互联网 发布:cda数据分析师相关数据 编辑:程序博客网 时间:2024/05/16 14:29

转载自http://www.cnblogs.com/hongten/p/hongten_python_os_apth.html

python中,os.path模块在处理路径的时候非常有用

下面是我做的demo

运行效果:

=========================================

代码部分:

=========================================

#python osimport osdef abspath(path):    '''Return a normalized absolutized version of the pathname path'''    return os.path.abspath(path)def dirname(path):    '''Return the directory name of pathname path'''    return os.path.dirname(path)def getatime(path):    '''Return the time of last access of path'''    return os.path.getatime(path)def gettime(path):    '''Return the time of last modification of path'''    return os.path.gettime(path)def getsize(path):    '''Return the size, in bytes, of path. Raise OSError if the file does not exist or is inaccessible.'''    return os.path.getsize(path)def is_file(path):    '''Return True if path is an existing regular file.    This follows symbolic links, so both islink() and isfile()    can be true for the same path.'''    return os.path.isfile(path)def is_dir(path):    '''Return True if path is an existing directory. This follows symbolic links,    so both islink() and isdir() can be true for the same path.'''    return os.path.isdir(path)def is_link(path):    '''Return True if path refers to a directory entry that    is a symbolic link. Always False if symbolic links are not supported.'''    return os.path.islink(path)def splitext(path):    '''    Split the pathname path into a pair (root, ext) such that    root + ext == path, and ext is empty or begins with a period    and contains at most one period. Leading periods on the basename    are ignored; splitext('.cshrc') returns ('.cshrc', '').    '''    return os.path.splitext(path)def splitunc(path):    '''    Split the pathname path into a pair (unc, rest) so that unc is    the UNC mount point (such as r'\\host\mount'), if present,    and rest the rest of the path (such as r'\path\file.ext').    For paths containing drive letters, unc will always be the    empty string.    '''    return os.path.splitunc(path)def split(path):    '''Split the pathname path into a pair, (head, tail) where tail is the last    pathname component and head is everything leading up to that'''    return os.path.split(path)    def main():    path_file = 'C:\\test.html'    path_dir = 'C:\\Windows\\Branding'    print(abspath(path_file))    print(dirname(path_dir))    print(getatime(path_file))    print(getsize(path_file))    print(splitext(path_file))    print(splitunc(path_file))    print(split(path_file))if __name__ == '__main__':    main()


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 六个月婴儿发烧怎么办退烧快点 咳嗽20天老不好怎么办 吃过退烧药出汗怎么办 5岁儿童发烧39度怎么办 小孩烧到39度怎么办 儿童7岁发烧39度怎么办 发烧没药怎么办怎样退烧快 小孩发烧怎么办怎样退烧快 发烧头疼怎么办最快最有效 发烧头晕怎么办最快最有效 婴儿发烧怎么办最快最有效 孩子一直37度8怎么办 一岁半宝宝37度5怎么办 发烧打了针35度怎么办 小孩发烧吃了鱼怎么办 八个月婴儿发烧39度怎么办 婴儿反复发烧39度怎么办 宝宝发烧了怎么办如何退烧 宝宝烧到38.8度怎么办 小孩发烧到39度怎么办 宝贝发烧到40度怎么办 孩子发烧39度8怎么办 儿子发烧39度该怎么办 孩子发烧39度7怎么办 7个月婴儿发烧怎么办 感冒了头发很油怎么办 5岁宝宝发烧39度怎么办 婴儿烧到39.5度怎么办 1岁多宝宝39.5度怎么办 宝宝发烧40多度怎么办 7岁宝宝发烧了怎么办 宝宝反复发烧39度怎么办 一岁半宝宝反复发烧39度怎么办 七岁发烧38度怎么办 小孩一直37度1怎么办 婴儿一直37度多怎么办 1岁半高烧39度怎么办 反复发烧39度多怎么办 孩子不爱喝水怎么办%3f 8岁儿童不爱喝水怎么办 儿子14岁了不爱说话怎么办