#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/6/13 15:03
# @File    : abspath_1.py

import os
import time

print('abspath-------------->', os.path.abspath(__file__))
# abspath--------------> D:\pytharm\jichuyufa\model3\practise3\abspath_1.py

print('split-------------->', os.path.split(__file__))
# 返回一个元祖 split--------------> ('D:/pytharm/jichuyufa/model3/practise3', 'abspath_1.py')

print('split-------------->', os.path.split(__file__)[0])
# 结果与dirname相同 split--------------> D:/pytharm/jichuyufa/model3/practise3

print('dirname-------------->', os.path.dirname(__file__))
# dirname--------------> D:/pytharm/jichuyufa/model3/practise3

print('basename-------------->', os.path.basename(__file__))
# 打印当前文件名称 basename--------------> abspath_1.py

li = ['/home/td', '/home/td/ff', '/home/td/fff']
print('commonprefix------------>', os.path.commonprefix(li))
# 返回list中,所有path共有的最长的路径。 commonprefix------------> /home/td

pa = r'E:\fmgao\2018高凤明\2018\新企业\message.txt'
print('exists------------->', os.path.exists(pa))
# 如果path在本机(不一定是项目中路劲)存在,返回True;如果path不存在,返回False。

print('isabs------------->', os.path.isabs(pa))
# 如果path是绝对路径,返回True。

print('isfile-------------->', os.path.isfile(pa))
# 如果path是一个存在的文件(目录不行,False),返回True。否则返回False

print('isdir--------------->', os.path.isdir(pa))
# 如果path是一个存在的目录(文件不行,False),返回True。否则返回False

print('join------------->', os.path.join('alex', 'get', 'e.txt'))
# join-------------> alex\get\e.txt

pa1 = 'C:/windows\\system32\\'
print('normcase-------------->', os.path.normcase(pa1))
# 在Linux和Mac平台上,该函数会原样返回path,在windows平台上会将路径中所有字符转换为小写,并将所有斜杠转换为反斜杠
# 不加r normcase--------------> c:\windows\system32\   加r  c:\windows\\system32\\

print('normpath------------>', os.path.normpath(pa1))
# 规范路径 normpath------------> C:\windows\system32

print('splitdrive---------->', os.path.splitdrive(__file__))
# 返回(drivername,fpath)元组
# splitdrive----------> ('D:', '/pytharm/jichuyufa/model3/practise3/abspath_1.py')

print('splitext----------->', os.path.splitext(__file__))
# 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作
# splitext-----------> ('D:/pytharm/jichuyufa/model3/practise3/abspath_1', '.py')

print('getsize----------->', os.path.getsize(__file__))
# 返回path的文件的大小(字节) getsize-----------> 2707

print('getatime---------->', os.path.getatime(pa))
# 返回path所指向的文件或者目录的最后存取时间
# getatime----------> 1527660739.6674004

print('getctime---------->', os.path.getctime(pa))  # 创建
print('getmtime---------->', os.path.getmtime(pa))  # 修改

# 时间示例:
fileTimesOfAccess = time.localtime(os.path.getatime(__file__))

yearOfAccess = fileTimesOfAccess.tm_year
monthOfAccess = fileTimesOfAccess.tm_mon
dayOfAccess = fileTimesOfAccess.tm_mday

hourOfAccess = fileTimesOfAccess.tm_hour
minuteOfAccess = fileTimesOfAccess.tm_min
secondOfAccess = fileTimesOfAccess.tm_sec

print('文件最近访问时间:  ', yearOfAccess, '年', monthOfAccess, '月', dayOfAccess, '日', '  ', hourOfAccess, '时', minuteOfAccess,
      '分', secondOfAccess, '秒')

文件os.path相关方法的更多相关文章

  1. python学习笔记24(路径与文件 (os.path包, glob包))

    os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法. >>> import os.path >>> path = '/home/ ...

  2. Python标准库03 路径与文件 (os.path包, glob包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 路径与文件的简介请参看Linux文件系统 os.path包 os.path包主要是 ...

  3. python --标准库 路径与文件 (os.path包, glob包)

    os.path包 os.path包主要是处理路径字符串,提取出有用信息. #coding:utf-8 import os.path path = 'D:\\Python7\\test\\data.tx ...

  4. Python3 os.path() 模块笔记

    os.path 模块主要用于获取文件的属性. 以下是 os.path 模块的几种常用方法: 方法 说明 os.path.abspath(path) 返回绝对路径 os.path.basename(pa ...

  5. python os.path模块

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

  6. python os.path

    os.path 提供了一些处理文件路径的函数. os.path.abspath(path) 返回绝对路径, 在大多数平台上, os.path.abspath(path) == os.path.norm ...

  7. os.path 大全

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回一个路径的最后一个组成部分 os.path.commonprefix(list) #返回 ...

  8. python os.path 模块

    os.path模块用法: 1, os.path.basename() >>> os.path.basename('/share/Public/cmiao')'cmiao' basen ...

  9. Python 中 os.path模板

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

随机推荐

  1. IE8 JSON is not defined

    问题原因: 昨天遇到了一个问题.就是用ajax从后台查询数据时,返回信息无法显示,经过提示发现是IE控制台提示: JSON is not defined 错误. 而且这个问题在本人自己的电脑上是不存在 ...

  2. bzoj-4887-dp+矩阵快速幂

    4887: [Tjoi2017]可乐 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 247  Solved: 170[Submit][Status][D ...

  3. Oracle linux安装Oracle 11G

    Oracle  linux安装Oracle 11G 系统环境 Oracle linux   5.8.19.6(64位) Oracle     11.2.0.1(64位) 本文档详细介绍Oracle  ...

  4. qt忙等与非忙等

    非忙等: void delay(int msec) { QTime end = QTime::currentTime().addMSecs(msec); while( QTime::currentTi ...

  5. vim按下ctrl+s僵死

    CTRL+S表示停止向终端停止输出 CTRL+Q恢复向终端输出流

  6. json to xml

    /* This work is licensed under Creative Commons GNU LGPL License. License: http://creativecommons.or ...

  7. VMwarePlayer虚拟机下centos6的静态IP配置

    1. 把VMwarePlayer生成的网络适配器 VMware Network Adapter VMnet1 设置成自动获取IP地址. 2.在VMwarePlayer中选择对应的虚拟机,点击edit ...

  8. 使用Junit进行Java单元测试

    1.新建一个Number类,该类中包含两个函数,求和.求差 2.在eclipse上安装Junit 右键test工程,选择“Properties”→“Java Build Path”→“Librarie ...

  9. iOS-----使用AddressBook管理联系人

    使用AddressBook管理联系人 iPhone手机通常都是自带的Contacts应用,包括所有联系人的性(last name).名(first name).电话.E-mail地址.住址.生日等各种 ...

  10. BZOJ3298: [USACO 2011Open]cow checkers(佐威夫博弈)

    3298: [USACO 2011Open]cow checkers Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 195  Solved: 96[S ...