一、os 模块:调用操作系统命令
os 模块可以帮助我们直接对操作系统进行操作。我们可以直接调用操作系统的可执行文件、命令,直接操作文件、目录等等。在系统运维的核心基础。
os.system 可以帮助我们直接调用系统的命令
【示例】os.system 调用windows 系统的记事本程序
import os
os.system("notepad.exe")
【示例】os.system 调用windows 系统中ping 命令
import os
os.system("ping www.baidu.com")
运行结果:
正在Ping www.a.shifen.com [111.206.223.206] 具有32 字节的数据:
来自111.206.223.206 的回复: 字节=32 时间=9ms TTL=56
来自111.206.223.206 的回复: 字节=32 时间=7ms TTL=56
来自111.206.223.206 的回复: 字节=32 时间=6ms TTL=56
来自111.206.223.206 的回复: 字节=32 时间=9ms TTL=56
111.206.223.206 的Ping 统计信息:
数据包: 已发送= 4,已接收= 4,丢失= 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短= 6ms,最长= 9ms,平均= 7ms
【注】Linux 是命令行操作更容易,我们可以通过os.system 可以更加容易的调用相关的命令;
【注】控制台输出中文可能会有乱码问题,可以在file–>setting 中设置:
os.startfile:直接调用可执行文件
【示例】运行安装好的微信
import os
os.startfile(r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe")
运行结果:
二、os 模块:文件和目录操作
我们可以通过前面讲的文件对象实现对于文件内容的读写操作。
如果,还需要对文件和目录做其他操作,可以使用os 和os.path 模块。
2.1 os 模块下常用操作文件的方法
方法名 | 描述 |
---|---|
remove(path) | 删除指定的文件 |
rename(src,dest) | 重命名文件或目录 |
stat(path) | 返回文件的所有属性 |
listdir(path) | 返回path 目录下的文件和目录列表 |
2.2 os 模块下关于目录操作的相关方法,汇总如下:
方法名 | 描述 |
---|---|
mkdir(path) | 创建目录 |
makedirs(path1/path2/path3/…) | 创建多级目录 |
rmdir(path) | 删除目录 |
removedirs(path1/path2…) | 删除多级目录 |
getcwd() | 返回当前工作目录:current work dir |
chdir(path) | 把path 设为当前工作目录 |
walk() | 遍历目录树 |
sep | 当前操作系统所使用的路径分隔符 |
【示例】os 模块:创建、删除目录、获取文件信息等
# 测试os 模块中,关于文件和目录的操作
import os
#############获取文件和文件夹相关的信息################
print(os.name) # windows->nt linux 和unix->posix
print(os.sep) # windows->\ linux 和unix->/
print(repr(os.linesep)) # windows->\r\n linux-->\n\
print(os.stat("my02.py"))
##############关于工作目录的操作###############
# print(os.getcwd())
# os.chdir("d:") #改变当前的工作目录为:d:盘根目录
# os.mkdir("书籍")
################创建目录、创建多级目录、删除#############
# os.mkdir("书籍")
# os.rmdir("书籍") #相对路径都是相对于当前的工作目录
# os.makedirs("电影/港台/周星驰")
# os.removedirs("电影/港台/周星驰") #只能删除空目录
# os.makedirs("../音乐/香港/刘德华") #../指的是上一级目录
# os.rename("电影","movie")
dirs = os.listdir("movie")
print(dirs)
#############################
3、os.path模块-常用方法
os.path 模块提供了目录相关(路径判断、路径切分、路径连接、文件夹遍历)的操作
方法名 | 描述 |
---|---|
isabs(path) | 判断path 是否绝对路径 |
isdir(path) | 判断path 是否为目录 |
isfile(path) | 判断path 是否为文件 |
exists(path) | 判断指定路径的文件是否存在 |
getsize(filename) | 返回文件的大小 |
abspath(path) | 返回绝对路径 |
dirname§ | 返回目录的路径 |
getatime(filename) | 返回文件的最后访问时间 |
getmtime(filename) | 返回文件的最后修改时间 |
walk(top,func,arg) | 递归方式遍历目录 |
join(path,*paths) | 连接多个path |
split(path) | 对路径进行分割,以列表形式返回 |
splitext(path) | 从路径中分割文件的扩展名 |
【示例】测试os.path 中常用方法
# 测试os.path 常用方法
import os.path
#################获得目录、文件基本信息
######################
print(os.path.isabs("d:/a.txt")) # 是否绝对路径
print(os.path.isdir("d:/a.txt")) # 是否目录
print(os.path.isfile("d:/a.txt")) # 是否文件
print(os.path.exists("a.txt")) # 文件是否存在
print(os.path.getsize("a.txt")) # 文件大小
print(os.path.abspath("a.txt")) # 输出绝对路径
print(os.path.dirname("d:/a.txt")) # 输出所在目录
########获得创建时间、访问时间、最后修改时间##########
print(os.path.getctime("a.txt")) # 返回创建时间
print(os.path.getatime("a.txt")) # 返回最后访问时间
print(os.path.getmtime("a.txt")) # 返回最后修改时间
################对路径进行分割、连接操作####################
path = os.path.abspath("a.txt") # 返回绝对路径
print(os.path.split(path)) # 返回元组:目录、文件
('C:\\Users\\Administrator\\PycharmProjects\\mypro_io\\test_os', 'a.txt')
print(os.path.splitext(path)) # 返回元组:路径、扩展名
('C:\\Users\\Administrator\\PycharmProjects\\mypro_io\\test_os\\a', '.txt')
print(os.path.join("aa", "bb", "cc")) # 返回路径:aa/bb/cc
【示例】列出指定目录下所有的.py 文件,并输出文件名
# 列出指定目录下所有的.py 文件,并输出文件名
import os.path
path = os.getcwd()
file_list = os.listdir(path) # 列出子目录和子文件
for filename in file_list:
pos = filename.rfind(".")
if filename[pos + 1:] == "py":
print(filename, end="\t")
print("##################")
file_list2 = [filename for filename in os.listdir(path) if
filename.endswith(".py")]
for filename in file_list2:
print(filename, end="\t")
4、os模块-使用walk递归遍历
os.walk()方法:返回一个3 个元素的元组,(dirpath, dirnames, filenames)
- dirpath:要列出指定目录的路径
- dirnames:目录下的所有文件夹
- filenames:目录下的所有文件
【示例】使用walk()递归遍历所有文件和目录
import os
all_files = []
path = os.getcwd()
list_files = os.walk(path)
for dirpath, dirnames, filenames in list_files:
for dir in dirnames:
all_files.append(os.path.join(dirpath, dir))
for name in filenames:
all_files.append(os.path.join(dirpath, name))
# 打印子目录和子文件
for file in all_files:
print(file)
目录结构是:
运行结果:
C:\Users\Administrator\PycharmProjects\mypro_io\test_os\电影
C:\Users\Administrator\PycharmProjects\mypro_io\test_os\要看的书
C:\Users\Administrator\PycharmProjects\mypro_io\test_os\a.txt
C:\Users\Administrator\PycharmProjects\mypro_io\test_os\my01.py
C:\Users\Administrator\PycharmProjects\mypro_io\test_os\my02.py
C:\Users\Administrator\PycharmProjects\mypro_io\test_os\my03.py
C:\Users\Administrator\PycharmProjects\mypro_io\test_os\my04.py
C:\Users\Administrator\PycharmProjects\mypro_io\test_os\my05.py
C:\Users\Administrator\PycharmProjects\mypro_io\test_os\my06.py
C:\Users\Administrator\PycharmProjects\mypro_io\test_os\电影\学习
C:\Users\Administrator\PycharmProjects\mypro_io\test_os\电影\学习\尚学堂
C:\Users\Administrator\PycharmProjects\mypro_io\test_os\电影\学习\尚学堂\python