Python基础知识学习总结(八)


一、os模块

Python的os模块提供了帮你执行文件处理操作的方法,比如重命名和删除文件。

要使用这个模块,你必须先导入它,然后才可以调用相关的各种功能。

#加载
import os
#查看os下的函数
print(dir(os))
#查看os.path下的函数
print(dir(os.path))

1. 常用函数

1> rename() 方法

rename() 方法为重命名文件,需要两个参数,当前的文件名和新文件名。

import os

# 重命名文件password.txt到test.txt。
os.rename("password.txt", "test.txt")

2> remove()方法

你可以用remove()方法删除文件,需要提供要删除的文件名作为参数。

import os
 
# 删除一个已经存在的文件test.txt
os.remove("test.txt")

3> mkdir()方法

可以使用os模块的mkdir()方法在当前目录下创建新的目录们。你需要提供一个包含了要创建的目录名称的参数。

import os
 
# 创建目录test
os.mkdir("test")

4> chdir()方法

可以用chdir()方法来改变当前的目录。chdir()方法需要的一个参数是你想设成当前目录的目录名称。

import os
 
# 将当前目录改为"/home/newdir"
os.chdir("/home/newdir")

5> getcwd()方法

getcwd()方法显示当前的工作目录。

import os
 
# 给出当前的目录
print(os.getcwd())

6. rmdir()方法

rmdir()方法删除目录,目录名称以参数传递。

在删除这个目录之前,它的所有内容应该先被清除。

以下是删除" /tmp/test"目录的例子。目录的完全合规的名称必须被给出,否则会在当前目录下搜索该目录。

import os
 
# 删除”/tmp/test”目录
os.rmdir( "/tmp/test"  )

2. os具体函数即功能(39个)

函数描述语法示例
os.name()显示当前使用的平台,'nt’表示Windows,‘posix’ 表示Linuxos.name()'nt'
os.getcwd()返回当前进程的工作目录os.getcwd()'C:\\Users\\wuzhengxiang'
os.chdir(path)改变当前工作目录到指定的路径os.chdir(path)os.chdir('C:/Users/wuzhengxiang/Desktop/股票数据分析')
os.makedirs(path)递归创建目录,创建的所有 intermediate-level 文件夹需要包含子目录os.makedirs(path, mode=0o777)os.makedirs('C:/Users/wuzhengxiang/Desktop/股票数据分析/1122', mode=0o777)
os.mkdir(path)以数字权限模式创建目录,默认的模式为 0777os.mkdir(path[, mode])os.mkdir('C:/Users/wuzhengxiang/Desktop/股票数据分析/2233', mode=0o777)
os.listdir(path)列出目录下的所有文件和文件夹os.listdir(path)os.listdir('C:/Users/wuzhengxiang/Desktop/股票数据分析')
os.remove(path)删除指定路径的文件os.remove(path)os.remove('C:/Users/zhengxiang.wzx/Desktop/timg.jpg')
os.rename(src, dst)重命名文件或目录os.rename(src, dst)os.rename("图片下载.py","图片下载1.py")
os.renames(old, new)递归重命名目录或文件os.renames(old, new)os.renames("test/Python 63个内置函数详解.py","test2/内置函数详解.py")
os.linesep当前平台用于分隔(或终止)行的字符串os.linesep'\r\n'
os.pathsep操作系统用于分隔搜索路径中不同部分的字符os.pathsep';'
os.close(fd)关闭指定的文件描述符os.close(fd)`fd = os.open(“foo.txt”, os.O_RDWR
os.stat(path)获取文件或者目录信息os.stat(path)os.stat('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
os.sep显示当前平台下路径分隔符os.sep'\\'
os.path.abspath(path)返回文件的绝对路径os.path.abspath(path)os.path.abspath('all_data.xlsx')
os.path.basename(path)返回文件名,纯粹字符串处理逻辑,路径错误也可以os.path.basename(path)os.path.basename('C:\\Users\\zhengxiang.wzx\\all_data.xlsx')
os.path.commonprefix(list)返回多个路径中所有路径共有的最长的路径os.path.commonprefix(list)os.path.commonprefix(['http://c.biancheng.net/python/aaa', 'http://c.biancheng.net/shell/'])
os.path.dirname(path)返回文件路径os.path.dirname(path)os.path.dirname('C://my_file.txt')
os.path.exists(path)如果路径存在,返回 True;如果路径不存在,返回 Falseos.path.exists(path)os.path.exists('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
os.path.lexists(path)路径存在则返回True,路径损坏也返回True,不存在返回 Falseos.path.lexists(path)os.path.lexists('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
os.path.expanduser(path)把路径中包含的"~"和"user"转换成用户目录os.path.expanduser(path)os.path.expanduser('~/wuzhengxiang/Desktop/股票数据分析/')
os.path.expandvars(path)根据环境变量的值替换路径中包含的" n a m e " 和 " name"和" name""{name}"os.path.expandvars(path)os.environ['KITTIPATH'] = 'D:/thunder'; os.path.expandvars('$KITTIPATH/train/2011_09_26_drive_0001_sync/proj_depth/velodyne_raw/image_02/0000000013.png')
os.path.getatime(path)返回最近访问时间(浮点型秒数),从新纪元到访问时的秒数os.path.getatime(path)os.path.getatime('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
os.path.getmtime(path)返回最近文件修改时间,从新纪元到访问时的秒数os.path.getmtime(path)os.path.getmtime('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
os.path.getctime(path)返回文件创建时间,从新纪元到访问时的秒数os.path.getctime(path)os.path.getctime('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
os.path.getsize(path)返回文件大小,如果文件不存在则返回错误os.path.getsize(path)os.path.getsize('C:/Users/wuzhengxiang/Desktop/股票数据分析/test.gif')
os.path.isabs(path)判断是否为绝对路径os.path.isabs(path)os.path.isabs('D:/thunder')
os.path.isfile(path)判断路径是否为文件os.path.isfile(path)os.path.isfile('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
os.path.isdir(path)判断路径是否为目录os.path.isdir(path)os.path.isdir('C:/Users/wuzhengxiang/Desktop/股票数据分析')
os.path.join(path1, ...)把目录和文件名合成一个路径os.path.join(path1[, path2[, ...]])os.path.join('C:/Users','wuzhengxiang/Desktop/','股票数据分析')
os.path.normcase(path)转换路径的大小写和斜杠os.path.normcase(path)os.path.normcase('D:\\Python\\test\\data.txt')
os.path.normpath(path)规范化路径字符串形式os.path.normpath(path)os.path.normpath('c://windows\\System32\\../Temp/')
os.path.realpath(path)返回路径的真实路径os.path.realpath(path)os.path.realpath('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
os.path.relpath(path[, start])返回从当前目录或 start 目录到达 path 之间要经过的相对路径os.path.relpath(path[, start])os.path.relpath('C:/Users/wuzhengxiang/Desktop/股票数据分析/test.gif')
os.path.samefile(path1, path2)判断目录或文件是否相同os.path.samefile(path1, path2)os.path.samefile('C:\\Users', 'C:/Users')
os.path.split(path)把路径分割成目录名和文件名,返回一个元组os.path.split(path)os.path.split('D:\\Python\\test\\data.txt')
os.path.splitdrive(path)返回驱动器名和路径组成的元组os.path.splitdrive(path)os.path.splitdrive('C:/Users/zhengxiang.wzx/IMG_7358.JPG')
os.path.splitext(path)分割路径,返回路径名和文件扩展名的元组os.path.splitext(path)os.path.splitext('C:/Users/zhengxiang.wzx/IMG_7358.JPG')
  • 12
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

晚睡早起₍˄·͈༝·͈˄*₎◞ ̑̑

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

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

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

打赏作者

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

抵扣说明:

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

余额充值