Python 文件路径path.os操作

文件路径os.path基本使用

os.path.exists(path)
        Test whether a path exists.  Returns False for broken symbolic links.
os.path.getatime(filename)
        Return the last access time of a file, reported by os.stat().
os.path.getctime(filename)
        Return the metadata change time of a file, reported by os.stat().
os.path.getmtime(filename)
        Return the last modification time of a file, reported by os.stat().
os.path.getsize(filename)
        Return the size of a file, reported by os.stat().
os.path.isfile(path)
        Test whether a path is a regular file.
os.path.samefile(f1, f2)
        Test whether two pathnames reference the same actual file.
os.path.split(p)
        Split a pathname.
        Return tuple (head, tail) where tail is everything after the final slash.
        Either part may be empty.

os.path.exists(path)
        判断路径是否存在
os.path.getatime(filename)
        返回最后一次访问时间
os.path.getctime(filename)
        返回最后一次文件数据(不包含文件属性)修改时间
os.path.getmtime(filename)
        返回最后一次文件修改时间
os.path.getsize(filename)
        返回文件的大小
os.path.isfile(path)
        判断是否是常规文件(而不是文件夹)
os.path.samefile(f1, f2)
        判断是否同一个文件
os.path.split(p)
        文件名分割,(head, tail),head是其父路径,tail是其去掉路径后的文件名,如('F:\\', 'test.txt')

补充说明

时间的单位是秒的浮点数,文件大小的单位是字节Byte
记得导入os模块
pycharm中按住Ctrl不放,点击函数可查看详细api

发布了18 篇原创文章 · 获赞 0 · 访问量 672

猜你喜欢

转载自blog.csdn.net/weixin_45792450/article/details/103936217
今日推荐