# operation system 操作系统
# os模块获取电脑的相关信息
# 并且有很强大的文件及文件夹操作能力
# 所以在操作文件或者文件夹的时候
# 首先要引入os模块
import os
# 获取电脑cpu个数
cpuCount = os.cpu_count()
print(cpuCount)
name = os.name
# nt代表windows操作系统 linux为posix
print('操作系统的名字是:{}'.format(name))
# exists存在 path路径
# 相对路径
result = os.path.exists('1.homework.py')
if result :
print('存在')
else :
print('不存在')
print(result)
# C:\Users\a\Desktop\os测试
# C:/Users/a/Desktop/os测试 adminstrator
result = os.path.exists('C:/Users/a/Desktop/os测试/python.txt')
print(result)
# 当前文件的绝对路径
result = os.getcwd()
print(result)
# absolute 绝对的
# 在计算机当中,获取当前文件路径 用.
# 获取父文件夹路径 用 ..
result = os.path.abspath('.')
print(result)
result = os.path.abspath('..')
print(result)
# 获取指定文件对应的绝对路径
result = os.path.abspath('周二.txt')
print(result)
# 获取文件路径的某一部分 C:/Users/a/Desktop/os测试
result = os.path.basename('C:/Users/a/Desktop/os测试')
print('路径的basename:{}'.format(result))
# common 公共的
result = os.path.commonpath(['C:/Users/a/Desktop/os测试',
'C:/Users/a/Desktop/同屏',
'C:/Users/a/Desktop/文件夹集合'])
print('路径的公共部分为:{}'.format(result))
# 注意:以/分割 将路径分成几部分 找到公共的这一个部分
result = os.path.commonpath(['http://www.baidu.com',
'http://www.jd.com',
'http://www.taobao.com'])
print('网址的公共部分为:{}'.format(result))
# directory name 获取指定文件所在的文件夹路径
result = os.path.dirname('C:/Users/a/Desktop/os测试/python.txt')
print(result)
# 获取文件夹 信息 -----------------------------------
# 文件夹信息包括 创建日期 修改日期 访问日期
import time
# getctime get获取
# c 文档是:change 实际是:create
result = os.path.getctime('C:/Users/a/Desktop/os测试')
print('文件创建日期为:{}'.format(time.localtime(result)))
# a : access 访问
result = os.path.getatime('C:/Users/a/Desktop/os测试/')
print('文件的访问日期是:{}'.format(time.localtime(result)))
# m :modify 修改
result = os.path.getmtime('C:/Users/a/Desktop/os测试')
print('文件的修改日期是:{}'.format(time.localtime(result)))
# size 尺寸;大小 # 获取的大小 为字节大小 B
result = os.path.getsize('C:/Users/a/Desktop/os测试')
# KB
# 1TB = 1024GB 32G 30G
# 1G = 1024M
# isFile 判断是否为文件
# os.path.exists()
result = os.path.isfile('C:/Users/a/Desktop/os测试/python.txt')
print('{}'.format(result))
# 文件分割-----------------------------------------
# split 分割;
# 分割路径
# 两部分
# 1 .除最后路径外的全部路径
# 2.最后路径
result = os.path.split('C:/Users/a/Desktop/os测试/python.txt')
print('{}'.format(result))
# 1.全部路径
# 2.文件后缀
result =os.path.splitext('C:/Users/a/Desktop/os测试/python.txt')
print('{}'.format(result))
# 文件夹增删改操作-----------------------------------
# 值1:修改前的名字
# 值2:修改后的名字
if os._exists('happy.txt'):
os.rename('happy.txt','葫芦娃.mp3')
if os._exists('葫芦娃.mp3'):
os.remove('葫芦娃.mp3')
# make directory
# os.mkdir('test')
# 小程序
# 1.随意输入一个输入 如果是1 创建一个文件夹名字为test_one
# 2.如果是2 删除一个文件夹 名字是test_one
# 3.如果是其他数字 返回
# while后面需要跟一个判断条件
# 条件为真的情况下 会一直执行
# 直到条件为假 或者跳出循环
while False :
num = input('请输入一个数字')
num = int(num)
if num == 1:
if os.path.exists('test_one'):
pass
else :
os.mkdir('test_one')
elif num == 2 :
if os.path.exists('test_one'):
os.removedirs('test_one')
else :
pass
else :
break
print('{}'.format(os.path.abspath('.')))
print('{}'.format(os.getcwd()))
# change 改变 改变当前所在的目录
os.chdir('test')
print('{}'.format(os.getcwd()))
os.removedirs('testA')