首页 > 代码库 > python基础学习日志day5---os模块

python基础学习日志day5---os模块

python os模块提供对操作系统进行调用的接口。

# -*- coding:utf-8 -*-
__author__ = ‘shisanjun‘

import os

print(os.getcwd())#获取当前工作目录,即当前python脚本工作的目录路径

os.chdir("F:\python运维开发\day4")#改变当前的工作目录:相当于shell下cd
print(os.getcwd())#结果F:\python运维开发\day4

os.chdir(os.curdir)#返回当前目录:(.):相当于shell下cd .
print(os.getcwd())

os.chdir(os.pardir)#返回当前父目录:(..):相当于shell下cd ..
print(os.getcwd())

os.makedirs("day6/test")#可生成多层递归目录:相当于shell下mkdir -r day6/test,目录存在报错
os.removedirs("day6/test")#删除多层递归目录:相当于shell下rm -rf day6/test

#os.mkdir("day6")#可生成单级目录:相当于shell下mkdir day6
#os.rmdir("day6")#可删除单级目录:相当于shell下rm -f day6

print(os.listdir("F:\python运维开发"))#列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 ls -a

os.chdir("F:\python运维开发\day6")
#os.remove("1.py")#删除文件
#os.rename("2.py","1.py")#重命名文件
print(os.stat("1.py"))# 获取文件或目录信息
print(os.sep)#输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
print(os.linesep)#输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
print(os.pathsep)#输出用于分割文件路径的字符串,win下为(:)
print(os.name)#输出字符串指示当前使用平台。win->‘nt‘; Linux->‘posix‘
os.system("ls -l") #运行shell命令,直接显示
print(os.environ)# 获取系统环境变量
print(os.path.abspath(os.curdir))#返回path规范化的绝对路径
print(os.path.split(‘F:\python运维开发\day6\\test1.py‘))#将path分割成目录和文件名二元组返回
print(os.path.basename(‘F:\python运维开发\day6\\test1.py‘))#返回path的目录。其实就是os.path.split(path)的第一个元素
print(os.path.dirname(‘F:\python运维开发\day6\\test1.py‘))#返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素


print(os.path.exists("F:\python运维开发\day6"))#如果path存在,返回True;如果path不存在,返回False
print(os.path.isabs("F:\python运维开发\day6")) # 如果path是绝对路径,返回True
print(os.path.isabs("python运维开发\day6"))
print(os.path.isfile(‘F:\python运维开发\day6\\1.py‘))#如果path是一个存在的文件,返回True。否则返回False

print(os.path.isdir("F:\python运维开发\day6"))#如果path是一个存在的目录,则返回True。否则返回False

print(os.path.join(os.curdir,"1.py"))# 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略

print(os.path.getatime(os.curdir))# 返回path所指向的文件或者目录的最后存取时间,是个时间戳
print(os.path.getmtime(os.curdir))# 返回path所指向的文件或者目录的最后修改时间,是个时间戳

python基础学习日志day5---os模块