Python 学习笔记 -- os

在做项目的过程中,预处理是比较麻烦的事情,一开始对python里os操作也不是很了解,所以还是查了很多资料。本文对一些常用的os function进行总结,我最主要用到的还是os.path里的一些方法,先做一个梳理。

os.path

os.path.join()

用于拼接文件的路径,是str类型:

  1. 往前看,会从第一个以“/”开头的参数开始拼接,把之前的参数全部丢弃。
  2. “./”在程序中屏蔽了“/”,使“/”不起任何作用。
  3. os.path.join()的功能只是起到连接文件的作用,而不能生成文件
    eg :
import os
 
a = os.path.join('aaaa', '/bbbb', 'test.txt')
b = os.path.join('/aaaa', '/bbbb', '/test.txt')
# join error
c = os.path.join('aaaa', './bbbb', 'test.txt')
d = os.path.join('aaaa', 'bbbb', 'dddd', './cccc', 'test.txt')
print('a:', a)
print('b:', b)
print('c:', c)
print('d:', d)

输出结果为:

a: /bbbb/test.txt
b: /test.txt
c: aaaa/./bbbb/test.txt
d: aaaa/bbbb/dddd/./cccc/test.txt

os.path.split()

返回路径的目录和文件名,是tuple结构。
此处只是把前后两部分分开而已。就是找最后一个’/’,例三就是个特例。
eg:

>>> os.path.split("/home/projects/helloworld.py")
>('/home/projects', 'helloworld.py')

>>> type(os.path.split("/home/projects/helloworld.py"))
> <class 'tuple'>

>>> type(os.path.split("/home/projects/"))
>('/home/projects', '')

os.path – query functions

查询:返回值True,False

  1. exists() – 指定路径(文件或者目录)是否存在
  2. isabs() – 指定路径是否为绝对路径
  3. isdir() – 指定路径是否存在且为一个目录
  4. isfile() – 指定路径是否存在且为一个文件
  5. islink() – 指定路径是否存在且为一个符号链接
  6. ismount() – 指定路径是否存在且为一个挂载点
  7. samefile() – 两个路径名是否指向同一个文件

os.path.exists()

检验指定的对象是否存在,返回值为True或False。

>>> os.path.exists("./home")
True

os.path.isfile()

判断指定对象是否为文件,返回值为True或False。

os.path.isdir()

判断指定对象是否为目录,返回值为True或False。

… …

其他就不举例子的。

os.path – file info functions

文件信息操作

  1. getatime() 返回最近访问时间 (浮点型秒数)
  2. getctime() 返回文件创建时间
  3. getmtime() 返回最近文件修改时间
  4. getsize() 返回文件大小 (字节为单位)
  5. abspath() 返回绝对路径
  6. normpath() 规范化路径

os.path.getsize()

获得文件的大小(字节为单位),返回值是int类型。

>>> print(os.path.getsize(file))  #假设文件是10926字节
10926 

os.path–about time

返回值是float类型。

print(os.path.getatime(file))   #输出最近访问时间
print(os.path.getctime(file))   #输出文件创建时间
print(os.path.getmtime(file))   #输出最近修改时间

os.path–about path

返回值是str类型。

print(os.path.abspath(file))    #输出绝对路径
print(os.path.normpath(file))   #输出规范化路径

print(type(os.path.abspath(file)))
<class 'str'>

os.path.basename(path)

返回文件名。

os.path.dirname(path)

返回文件路径

os.mkdir()

以数字权限模式创建目录,默认的模式为 0777 (八进制),但是只能建立一层。

 >>> os.mkdir(path[, mode])
 '''
 	path -- 要创建的目录 
 	mode -- 要为目录设置的权限数字模式
 '''

如果想要递归创建目录,使用makedirs方法:

>>> os.makedirs(name [, mode=0o777][, exist_ok=False])

不常用的一些OS方法

os.name

后面没有括号,判断现在正在实用的平台,Windows 返回 ‘nt’; Linux/Unix/MacOS 返回’posix’

os.getcwd()

得到当前工作的目录。

os.listdir()

指定所有目录下所有的文件和目录名。

os.remove()

删除指定文件。

os.rmdir()

删除指定目录。

os.getcwd()

获得当前工作的目录(get current work dir)。

os.system()

执行shell命令。

os.chdir()

改变目录到指定目录

Reference

http://www.runoob.com/python/os-file-methods.html
https://my.oschina.net/cuffica/blog/33494
https://www.cnblogs.com/liuchunxiao83/p/5286638.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IndexFziQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值