#首先导入os包
import os
#引入time模块是因为需要将浮点型的时间转为东八区时间
import time # basename(path),去掉路径名称,单独返回文件名
f = os.path.basename('E:/python/day-2/test.txt') # 输出结果是test.txt
print(f) #dirname(path),去掉文件名称,单独返回目录路径
url = os.path.dirname('E:/python/day-2/test.txt') #打印除目录路径
print(url) #join(path1[,path2[,...]]) 将path1,path2各部分组合成一个路径名
joinpath = os.path.join('E:/','/day-2/') #打印出路径
print(joinpath) #split(path),分割文件名与文件路径,返回(f_path,f_name)元组。
#如果完全使用目录,它也会将最后一个目录作为文件名分离。
#且不会判断文件或者目录是否存在
splits = os.path.split('E:/python/day-2/test.txt') #打印结果 是('E:/python/day-2','test.txt')
print(splits) # splitext(path)分离文件名与扩展名,返回(f_name,f_extension)元组
splitexts = os.path.splitext('E:/python/day-2/test.txt') # 输出('E:/python/day-2/test','txt')
print(splitexts) #getsize(path)获取path下的文件大小,返回的是字节。
getsizes = os.path.getsize('E:/java/张鹏(个人简历).docx') # 输出字节
print(getsizes) # getatime(path) 获取文件的最后访问时间,返回浮点型的秒数
# 当我们想要将浮点型的秒数转为东八区区时时,通过引入 imoprt time
# time.localtime(getatimes)即可获得
getatimes = os.path.getatime('E:/java/张鹏(个人简历).docx')
print('输出浮点型的时间格式:',getatimes)
print("输出东八区时间:",time.localtime(getatimes)) # getctime(file),获取文件创建时间,返回浮点型秒数
getctimes = os.path.getctime('E:/java/张鹏(个人简历).docx')
print(time.localtime(getctimes)) # getmtime(file) 返回指定文件最新的修改时间,浮点型
getmtimes = os.path.getmtime('E:/java/张鹏(个人简历).docx')
print(time.localtime(getmtimes)) #exists(path)判定文件是否存在
exist = os.path.exists('E:/java/张鹏(个人简历).docx')
print('这是个存在的文件,他是否是True呢',exist) #这个文件不存在
exist = os.path.exists('E:/java/张鹏(个人简历)1.docx')
print("这是个不存在的文件,他是否是False呢",exist) #判断是否是绝对路径
print(os.path.isabs('E:/java/张鹏(个人简历)1.docx'))

#首先导入os包import osimport time
# basename(path),去掉路径名称,单独返回文件名f = os.path.basename('E:/python/day-2/test.txt')
# 输出结果是test.txtprint(f)
#dirname(path),去掉文件名称,单独返回目录路径url = os.path.dirname('E:/python/day-2/test.txt')
#打印除目录路径print(url)
#join(path1[,path2[,...]]) 将path1,path2各部分组合成一个路径名joinpath = os.path.join('E:/','/day-2/')
#打印出路径print(joinpath)
#split(path),分割文件名与文件路径,返回(f_path,f_name)元组。#如果完全使用目录,它也会将最后一个目录作为文件名分离。#且不会判断文件或者目录是否存在splits = os.path.split('E:/python/day-2/test.txt')
#打印结果 是('E:/python/day-2','test.txt')print(splits)
# splitext(path)分离文件名与扩展名,返回(f_name,f_extension)元组splitexts = os.path.splitext('E:/python/day-2/test.txt')
# 输出('E:/python/day-2/test','txt')print(splitexts)
#getsize(path)获取path下的文件大小,返回的是字节。getsizes = os.path.getsize('E:/java/张鹏(个人简历).docx')
# 输出字节print(getsizes)
# getatime(path) 获取文件的最后访问时间,返回浮点型的秒数# 当我们想要将浮点型的秒数转为东八区区时时,通过引入 imoprt time# time.localtime(getatimes)即可获得getatimes = os.path.getatime('E:/java/张鹏(个人简历).docx')print('输出浮点型的时间格式:',getatimes)print("输出东八区时间:",time.localtime(getatimes))
# getctime(file),获取文件创建时间,返回浮点型秒数getctimes = os.path.getctime('E:/java/张鹏(个人简历).docx')print(time.localtime(getctimes))
# getmtime(file) 返回指定文件最新的修改时间,浮点型getmtimes = os.path.getmtime('E:/java/张鹏(个人简历).docx')print(time.localtime(getmtimes))
#exists(path)判定文件是否存在exist = os.path.exists('E:/java/张鹏(个人简历).docx')print('这是个存在的文件,他是否是True呢',exist)
#这个文件不存在exist = os.path.exists('E:/java/张鹏(个人简历)1.docx')print("这是个不存在的文件,他是否是False呢",exist)
#判断是否是绝对路径print(os.path.isabs('E:/java/张鹏(个人简历)1.docx'))

python,os.path简单用法的更多相关文章

  1. python os.path模块用法详解

    abspath 返回一个目录的绝对路径 Return an absolute path. >>> os.path.abspath("/etc/sysconfig/selin ...

  2. python os.path 模块

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

  3. 【308】Python os.path 模块常用方法

    参考:Python os.path 模块 参考:python3中,os.path模块下常用的用法总结 01   abspath 返回一个目录的绝对路径. 02   basename 返回一个目录的基名 ...

  4. Python3 join函数和os.path.join用法

    Python3  join函数和os.path.join用法 os.path.join()连接两个文件名地址的时候,就比os.path.join("D:\","test. ...

  5. Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数

    Python  os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 的区别 os.path.abspath(__file__)返回 ...

  6. python os.path 的使用

    import os #该文件所在位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py path1 = os.path.dirname(__file__) print(path1)#获 ...

  7. Python os.path.dirname(__file__) os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  8. Python——os.path.dirname(__file__) 与 os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  9. python lambda表达式简单用法【转】

    python lambda表达式简单用法 1.lambda是什么? 看个例子: g = lambda x:x+1 看一下执行的结果: g(1) >>>2 g(2) >>& ...

随机推荐

  1. ABA问题

    CAS:对于内存中的某一个值V,提供一个旧值A和一个新值B.如果提供的旧值V和A相等就把B写入V.这个过程是原子性的.CAS执行结果要么成功要么失败,对于失败的情形下一班采用不断重试.或者放弃. AB ...

  2. Linux 普通用户免密码切换到root用户

    Linux 普通用户免密码切换到root用户 # 添加用户 useradd user_name # 修改密码 echo "user_name@pwd" | passwd --std ...

  3. hihocoder图像算子(高斯消元)

    描述 在图像处理的技术中,经常会用到算子与图像进行卷积运算,从而达到平滑图像或是查找边界的效果. 假设原图为H × W的矩阵A,算子矩阵为D × D的矩阵Op,则处理后的矩阵B大小为(H-D+1) × ...

  4. flask form表单验证

    新建forms.py文件 #!/usr/bin/env python #-*-coding:utf--*- #导入模块 from flask_wtf import FlaskForm #FlaskFo ...

  5. SpringBoot笔记十五:任务

    目录 异步任务 定时任务 异步任务 注解:@Async,@EnableAsync 我新建一个Service,就叫AsyncService package com.example.service; im ...

  6. Spring Boot笔记六:Thymeleaf介绍

    目录 什么是thymeleaf? 创建最简单的thymeleaf thymeleaf语法 什么是thymeleaf? thymeleaf是一个模板引擎,是用来在Spring Boot中代替JSP的 引 ...

  7. ipython介绍及使用

    1. IPython介绍 ipython是一个python的交互式shell,比默认的python shell好用得多,支持变量自动补全,自动缩进,支持bash shell命令,内置了许多很有用的功能 ...

  8. 多线程Thread类的方法

    创建多个线程的第一种方法 1.定义一个Thread类的子类,比如MyThread类 2.重写Thread的run方法,设置线程任务 3.创建Mythread类的对象 4.调用方法start(),开启新 ...

  9. C#中转换函数Convert、Parse、TryParse、(int) 的区别

    Convert.Parse.TryParse.(int) 三个函数都是将值转换成整数,但是四者之间各有异同,开发人员可以根据情况选用最合适的.以下解释均经过高人验证,希望对大家有所帮助. 1 (int ...

  10. 【一】java 虚拟机 监控示例 Eclipse Memory Analyser

    1.堆内存溢出示例代码 import java.util.ArrayList; import java.util.List; public class TestHeap { public static ...