京公网安备 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数据分析师 出品 作者:李诗怡 多表进阶与 DDL/DML 连接 · 子查询 · 合并 · 建表与书写规范 ” 三、DQL 多表进阶:连 ...
2026-11-20 前面讲了怎么搭指标体系,但很多团队卡在最后一步:高层定了一个漂亮的目标,往下一层层写进周报,最后却落不到任何人的具体 ...
2026-11-20 很多团队做数据分析的现状是:老板要一个数就拉一个数,周报里堆满了 GMV、DAU、转化率,却没人说得清这些数之间是什么关系 ...
2026-11-20这周你大概又经历过一次:报表自己跑了,SQL 让 AI 写了,连周报初稿都不用自己动手。你一边松口气,一边心里发慌——那我还剩什 ...
2026-11-20 做产品立项、写商业计划书、规划市场进入策略时,"市场调研"和"竞品分析"几乎总是被绑在一起说,甚至被当成同一件事。但实际 ...
2026-11-19 在做数据治理、搭数据中台或者梳理数据库表结构时,"主数据"和"参考数据"是被提及频率最高、也最容易被混为一谈的两个概念。 ...
2026-11-19“纲举目张,执本末从。”若想在数据分析领域有所收获,一套合适的学习教材至关重要。一套优质且契合需求的学习教材无疑是那关键 ...
2026-11-19一、什么是本体 在知识图谱和语义建模里,本体(Ontology)是对一个领域内概念、属性、关系和实例的形式化描述。它回答三个问题 ...
2026-11-18一、K-S 曲线解决什么问题 风控评分卡、客户流失模型、欺诈识别模型建好之后,最核心的一个问题是:模型能不能把"会变坏"和"不会 ...
2026-11-18核心资源推荐 CDA教材(2025):https://edu.cda.cn/goods/show/3151CDA模拟题(2025):https://edu.cda.cn/goods/show/2845CD ...
2026-11-18核心资源推荐 CDA教材(2025):https://edu.cda.cn/goods/show/3151CDA模拟题(2025):https://edu.cda.cn/goods/show/2845CD ...
2026-11-18产品上线之后,真正回答"用户是谁、想要什么、卡在哪里"的,往往不是调研问卷,而是用户留下的一条条行为记录。点击、浏览、停留 ...
2026-11-17在日常数据库开发与报表查询中,日期格式几乎是最高频的"打交道"对象:页面上要展示 yyyy-MM-dd,接口要返回 ISO 8601 字符串, ...
2026-11-17核心资源推荐 CDA教材(2025):https://edu.cda.cn/goods/show/3151CDA模拟题(2025):https://edu.cda.cn/goods/show/2845CD ...
2026-11-17一、销售行业为什么离不开大数据分析 销售行业的本质是"把合适的产品,在合适的时间,卖给合适的人,并持续复购"。传统销售靠经 ...
2026-11-16一、什么是频数分布 拿到一批连续数据(员工年龄、商品价格、考试分数、订单金额),最想先看的往往不是平均值,而是数据集中在 ...
2026-11-16教育部办公厅关于成立全国数据职业教育教学指导委员会(2026—2030年)的通知 教职成厅函〔2026〕25号 各省、自治区、直辖市教 ...
2026-11-16CDA数据分析师 出品 作者:李诗怡 基础与单表查询 四大类语言 · 数据库基础 · DQL 单表查询 ” 一、SQL 四大类语言与数据 ...
2026-11-13核心资源推荐 CDA教材(2025):https://edu.cda.cn/goods/show/3151CDA模拟题(2025):https://edu.cda.cn/goods/show/2845CD ...
2026-11-13一、问题从哪来 财务分析里算增长率,常规公式所有人都会写: 增长率本年数上年数上年数 这个公式在"上年为正"时完全成立。但一 ...
2026-11-12