import os
#获取电脑CPU个数
cpuCount=os.cpu_count()
print(cpuCount)name=os.name
#输出的name为nt nt代表Windows操作系统
而Linux为posix
print('操作系统的名字是:{}'.format(name))result = os.path.exists('1.作业.py')
if result:print('存在')
else:print('不存在')
#当py目录下存在该文件时,结果会输出存
#在,否则输出不存在。
result=os.getcwd()print(resulr)这是获取当前文件的路径result=os.path.abspath('.')#在计算机中,用 . 获取当前文件夹路径
print(result)
result=os.path.abspath('..')
print(result) #用..获取父文件夹路径
#获取指定文件的绝对路径
result=os.path.abspath('周二.txt')
print(result)
#获取文件路径的某一部分
#注意:路径一定要以/分割
result = os.path.basename('需要获取文件的路径')
print('路径的basename:{}'.format(result))
#获取文件夹信息。其中包括该文件的创建日期,修改日期,访问日期。
import time
#getctime 文件的创建日期 get获取
#c 文档是:change 实际是:crea
result = os.path.getctime('文件路径')
print(result)
#getatime 文件访问日期
result = os.path.getatime('文件路径')
print(result)
#getmtime 文件的修改日期
result = os.path.getmtime('文件路径')
print(result)
#getsize 文件的大小 单位为B
result = os.path.getsize(''文件路径)
print(result)
#文件夹增删改操作
值1:修改前的文件名字 值2:修改后的文件名字
import os
os.getcwd()
if os.path.exists('值1')#判断该文件是否存在
os.rename('值1','值2')
#os.remove('')删除指定问价 os.mkdir('')新建文件
小程序练习:
要求:随意输入一个数字,如果是1,执行创建一个文件夹名字为test_one.如果是2,执行删除一个文件夹名字为test_one.
如果是其他数字返回。
info=input('随意输入一个数字')
if info.isdigit():
info=int(info)
if info==1:
os.makedirs('text_one')
elif info==2:
os.removedirs('text_one')
else:
pass