
Python核心模块详解解之os模块_python os模块详解
Python核心模块详解解之os模块
os模块提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台,如posix或NT系统平台,os模块会根据不同的平台进行相应的操作.本节内容将对os模块提供的函数进行详细的解读.
1.1 文件操作函数
1.1.1 open()函数提供创建、打开、修改文件的功能。
Example 1-1. Using the os Module to Rename and Remove Files
#Filename: os-example-1.py
import os
import string
def replace(file, search_for, replace_with):
# replace strings in a text file
back = os.path.splitext(file)[0] + “.bak”
temp = os.path.splitext(file)[0] + “.tmp”
try:
# remove old temp file, if any
os.remove(temp)
except os.error:
pass
fi = open(file)
fo = open(temp, “w”)
for s in fi.readlines():
fo.write(string.replace(s, search_for, replace_with))
fi.close()
fo.close()
try:
# remove old backup file, if any
os.remove(back)
except os.error:
pass
# rename original to backup…
os.rename(file, back)
# …and temporary to original
os.rename(temp, file)
# try it out!
file = “samples/sample.txt”
replace(file, “hello”, “tjena”)
replace(file, “tjena”, “hello”)
1.1.2 rename()和remove()函数,对文件进行重命名和删除操作.请参照例1-1
1.2 目录操作
1.2.1 listdir()函数,返回指定目录下所有文件名,并保存于一列表中.但当前目录标记(.)和父目录标记(..)不在其中.
Example 1-2. Using the os Module to List the Files in a Directory
File: os-example-2.py
import os
for file in os.listdir(“samples”):
print file
1.2.2 getcwd()和chdir()函数,功能是获取和改变当前工作目录.
Example 1-3. Using the os Module to Change the Working Directory
#Filename: os-example-3.py
import os
# where are we?
cwd = os.getcwd()
print “1”, cwd
# go down
os.chdir(“samples”)
print “2”, os.getcwd()
# go back up
os.chdir(os.pardir)
print “3”, os.getcwd()
1.2.3 mkdir(),rmdir(),makedirs()和removedirs()函数用于创建和删除目录操作.mkdir,rmdir和makedirs,removedir的不同在于前者只创建和删除一级目录,而后者则能创建和删除多级目录.要删除非空目录则要用到shutil模块中的rmtree()函数,在shutil模块详解中有介绍.
Example 1-4. Create and Remove Multiple Directory Levels
#Filename: os-example-4.py
import os
os.makedirs(“test/multiple/levels”)
fp = open(“test/multiple/levels/file”, “w”)
fp.write(“inspector praline”)
fp.close()
# remove the file
os.remove(“test/multiple/levels/file”)
# and all empty directories above it
os.removedirs(“test/multiple/levels”)
1.3 文件属性操作
1.3.1stat()函数返回一个文件的所有属性,所有的属性都包含在一个元组中.而fstat()函数则返回一个已经打开的文件的所有属性.
Example 1-32. Get Information About a File数据分析培训
File: os-example-1.py
import os
import time
file = “samples/sample.jpg”
def dump(st):
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
print “- size:”, size, “bytes”
print “- owner:”, uid, gid
print “- created:”, time.ctime(ctime)
print “- last accessed:”, time.ctime(atime)
print “- last modified:”, time.ctime(mtime)
print “- mode:”, oct(mode)
print “- inode/dev:”, ino, dev
# get stats for a filename
st = os.stat(file)
print “station”, file
dump(st)
print st
# get stats for an open file
fp = open(file)
st = os.fstat(fp.fileno())
print “fstat”, file
dump(st)
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
t 检验与 Wilcoxon 检验:数据差异比较的两大统计利器 在数据分析中,“比较差异” 是核心需求之一 —— 如新药疗效是否优于旧药 ...
2025-08-26季节性分解外推法:解锁时间序列预测的规律密码 在商业决策、资源调度、政策制定等领域,准确的预测是规避风险、提升效率的关键 ...
2025-08-26CDA 数据分析师:数据治理驱动下的企业数据价值守护者 在数字经济时代,数据已成为企业核心战略资产,其价值的释放离不开高 ...
2025-08-26基于 SPSS 的 ROC 曲线平滑调整方法与实践指南 摘要 受试者工作特征曲线(ROC 曲线)是评估诊断模型或预测指标效能的核心工具, ...
2025-08-25神经网络隐藏层神经元个数的确定方法与实践 摘要 在神经网络模型设计中,隐藏层神经元个数的确定是影响模型性能、训练效率与泛 ...
2025-08-25CDA 数据分析师与数据思维:驱动企业管理升级的核心力量 在数字化浪潮席卷全球的当下,数据已成为企业继人力、物力、财力之后的 ...
2025-08-25CDA数据分析师与数据指标:基础概念与协同逻辑 一、CDA 数据分析师:数据驱动时代的核心角色 1.1 定义与行业价值 CDA(Certified ...
2025-08-22Power Query 移动加权平均计算 Power Query 移动加权平均设置全解析:从原理到实战 一、移动加权平均法的核心逻辑 移动加权平均 ...
2025-08-22描述性统计:CDA数据分析师的基础核心与实践应用 一、描述性统计的定位:CDA 认证的 “入门基石” 在 CDA(Certified Data Analy ...
2025-08-22基于 Python response.text 的科技新闻数据清洗去噪实践 在通过 Python requests 库的 response.text 获取 API 数据后,原始数据 ...
2025-08-21基于 Python response.text 的科技新闻综述 在 Python 网络爬虫与 API 调用场景中,response.text 是 requests 库发起请求后获取 ...
2025-08-21数据治理新浪潮:CDA 数据分析师的战略价值与驱动逻辑 一、数据治理的多维驱动引擎 在数字经济与人工智能深度融合的时代,数据治 ...
2025-08-21Power BI 热力地图制作指南:从数据准备到实战分析 在数据可视化领域,热力地图凭借 “直观呈现数据密度与分布趋势” 的核心优势 ...
2025-08-20PyTorch 矩阵运算加速库:从原理到实践的全面解析 在深度学习领域,矩阵运算堪称 “计算基石”。无论是卷积神经网络(CNN)中的 ...
2025-08-20数据建模:CDA 数据分析师的核心驱动力 在数字经济浪潮中,数据已成为企业决策的核心资产。CDA(Certified Data Analyst)数据分 ...
2025-08-20KS 曲线不光滑:模型评估的隐形陷阱,从原因到破局的全指南 在分类模型(如风控违约预测、电商用户流失预警、医疗疾病诊断)的评 ...
2025-08-20偏态分布:揭开数据背后的非对称真相,赋能精准决策 在数据分析的世界里,“正态分布” 常被视为 “理想模型”—— 数据围绕均值 ...
2025-08-19CDA 数据分析师:数字化时代的价值创造者与决策智囊 在数据洪流席卷全球的今天,“数据驱动” 已从企业战略口号落地为核心 ...
2025-08-19CDA 数据分析师:善用 Power BI 索引列,提升数据处理与分析效率 在 Power BI 数据分析流程中,“数据准备” 是决定后续分析质量 ...
2025-08-18CDA 数据分析师:巧用 SQL 多个聚合函数,解锁数据多维洞察 在企业数据分析场景中,单一维度的统计(如 “总销售额”“用户总数 ...
2025-08-18