京公网安备 11010802034615号
经营许可证编号:京B2-20210330
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
主讲人简介 张冲,海归统计学硕士,CDA 认证数据分析师,前云南白药集团资深数据分析师,自媒体 Python 讲师,全网课程播放量破 ...
2026-04-10在数据可视化与业务分析中,同比分析是衡量业务发展趋势、识别周期波动的核心手段,其核心逻辑是将当前周期数据与上年同期数据进 ...
2026-04-10在机器学习模型的落地应用中,预测精度并非衡量模型可靠性的唯一标准,不确定性分析同样不可或缺。尤其是在医疗诊断、自动驾驶、 ...
2026-04-10数据本身是沉默的,唯有通过有效的呈现方式,才能让其背后的规律、趋势与价值被看见、被理解、被运用。统计制图(数据可视化)作 ...
2026-04-10在全球化深度发展的今天,跨文化传播已成为连接不同文明、促进多元共生的核心纽带,其研究核心围绕“信息传递、文化解读、意义建 ...
2026-04-09在数据可视化领域,折线图是展示时序数据、趋势变化的核心图表类型之一,其简洁的线条的能够清晰呈现数据的起伏规律。Python ECh ...
2026-04-09在数据驱动的时代,数据分析早已不是“凭经验、靠感觉”的零散操作,而是一套具备固定逻辑、标准化流程的系统方法——这就是数据 ...
2026-04-09长短期记忆网络(LSTM)作为循环神经网络(RNN)的重要改进模型,凭借其独特的门控机制(遗忘门、输入门、输出门),有效解决了 ...
2026-04-08在数据分析全流程中,数据质量是决定分析结论可靠性的核心前提,而异常值作为数据集中的“异类”,往往会干扰统计检验、模型训练 ...
2026-04-08在数字经济飞速发展的今天,数据已渗透到各行各业的核心场景,成为解读趋势、优化决策、创造价值的核心载体。而数据分析,作为挖 ...
2026-04-08在数据分析全流程中,数据处理是基础,图形可视化是核心呈现手段——前者负责将杂乱无章的原始数据转化为干净、规范、可分析的格 ...
2026-04-07在数据分析与统计推断中,p值是衡量假设检验结果显著性的核心指标,其本质是在原假设(通常为“无效应”“无差异”)成立的前提 ...
2026-04-07在数字经济深度渗透的今天,数据已成为企业生存发展的核心资产,企业的竞争本质已转变为数据利用能力的竞争。然而,大量来自生产 ...
2026-04-07Python凭借简洁的语法、丰富的生态库,成为算法开发、数据处理、机器学习等领域的首选语言。但受限于动态类型、解释性执行的特性 ...
2026-04-03在深度学习神经网络中,卷积操作是实现数据特征提取的核心引擎,更是让模型“看懂”数据、“解读”数据的关键所在。不同于传统机 ...
2026-04-03当数字化转型从企业的“战略口号”落地为“生存之战”,越来越多的企业意识到,转型的核心并非技术的堆砌,而是数据价值的深度挖 ...
2026-04-03在日常办公数据分析中,数据透视表凭借高效的汇总、分组功能,成为Excel、WPS等办公软件中最常用的数据分析工具之一。其中,“计 ...
2026-04-02在数字化交互的全场景中,用户的每一次操作都在生成动态的行为轨迹——电商用户的“浏览商品→点击详情→加入购物车”,内容APP ...
2026-04-02在数字化转型深度推进的今天,企业数据已成为驱动业务增长、构建核心竞争力的战略资产,而数据安全则是守护这份资产的“生命线” ...
2026-04-02在数据驱动决策的浪潮中,数据挖掘与数据分析是两个高频出现且极易被混淆的概念。有人将二者等同看待,认为“做数据分析就是做数 ...
2026-04-01