os模块
print(os.name) #返回当前使用平台的代表字符,Windows用 nt 表示,Linux用 posix 表示,java虚拟机 java表示
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd
os.curdir 返回当前目录: ('.')
os.pardir 获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2') 可生成多层递归目录
os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove() 删除一个文件
os.rename("oldname","newname") 重命名文件/目录
os.stat('path/filename') 获取文件/目录信息
os.sep 输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep 输出当前平台使用的行终止符,win下为"\r\n",Linux下为"\n",mac"\n"
os.pathsep 输出用于分割文件路径的字符串 win下为;,Linux下为:
os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command") 运行shell命令,直接显示
os.environ 获取系统环境变量
os.path.abspath(path) 返回path规范化的绝对路径
os.path.splitext() 分离文件名与扩展名也是2元组 ('home_work\\work_0624', '.py')
os.path.split(path) 将path分割成目录和文件名二元组返回
print(os.path.split('Second_module\os模块.py')) (Second_module', 'os模块.py')
os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素
os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回字符串为空值""。即os.path.split(path)的第二个元素
os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path) 如果path是绝对路径,返回True
os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
os.path.getctime(filename) 返回文件的创建时间
os.path.getsize(path) 返回文件path的大小
# -*- coding: utf-8 -*-
"""
===========================
# @Time : 2020/8/25 18:28
# @File : os_.py
# @Author: adeng
# @Date : 2020/8/25
============================
"""
import os,sys
print(os.curdir) # .
print(os.pardir) # ..
# os.path.getsize()
os.environ["aaaaa"] = "bbbbbb"
print(os.environ["aaaaa"]) # bbbbbb
os.environ.setdefault("ccccc","11111111111111")
# print(os.environ) # 环境变量 用处 如果一个值需要在整个软件使用,可以加到os.
# os.environb
print(sys.platform)
"""
windows--- "win32"
linux --- "linux"
Mac---- "darwin"
"""
# 写一个功能 统计文件夹大小
# 方法1
def count_dir_size(path):
"""统计文件或者文件夹大小"""
count_size = 0
if not os.path.isdir(path):
return os.path.getsize(path)
else:
for root,dirs,files in os.walk(path):
pass
for f in files:
file_path = os.path.join(root,f)
count_size += os.path.getsize(file_path)
return count_size
print(count_dir_size(r"I:\markdown\模块"))
print(count_dir_size(r"I:\python20\softwaredate\py_basics\09 函数\dedai.txt"))
# 方法:递归
def count_dir_size(path):
""""统计文件或者文件夹大小,返回的是字节"""
count_size = 0
if not os.path.isdir(path):
return os.path.getsize(path)
else:
for f in os.listdir(path):
new_path = os.path.join(path,f)
if os.path.isfile(new_path):
count_size += os.path.getsize(new_path)
else:
count_size += count_dir_size(new_path)
return count_size
print(count_dir_size(r"I:\markdown\模块"))
print(count_dir_size(r"I:\python20\softwaredate\py_basics\09 函数\dedai.txt"))
a ="I:\markdown\模块\\"
a = a.rstrip("\\")
t = os.path.basename(a)
print(t) # "" 字符串为空
print(os.getcwd())
print(os.path.join("a","b","c"))
print(os.path.join("a","D:","b","c")) # 是从一个根作为起始位置,如果没有根那么按照顺序拼接
print(os.path.normpath("a/c/d/e/../..")) # a\c
Base_Dir = os.path.normpath(os.path.join(__file__,"..","..","..")) # I:\python20\softwaredate\py_basics
print(Base_Dir)
# 路径规范化
a = "c:/d\\c/3\\f\\bb.text"
a = os.path.normcase(a) # c:\d\c\3\f\bb.text
print(a)
网友评论