京公网安备 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
在数据处理的全流程中,数据呈现与数据分析是两个紧密关联却截然不同的核心环节。无论是科研数据整理、企业业务复盘,还是日常数 ...
2026-03-06在数据分析、数据预处理场景中,dat文件是一种常见的二进制或文本格式数据文件,广泛应用于科研数据、工程数据、传感器数据等领 ...
2026-03-06在数据驱动决策的时代,CDA(Certified Data Analyst)数据分析师的核心价值,早已超越单纯的数据清洗与统计分析,而是通过数据 ...
2026-03-06在教学管理、培训数据统计、课程体系搭建等场景中,经常需要对课时数据进行排序并实现累加计算——比如,按课程章节排序,累加各 ...
2026-03-05在数据分析场景中,环比是衡量数据短期波动的核心指标——它通过对比“当前周期与上一个相邻周期”的数据,直观反映指标的月度、 ...
2026-03-05数据治理是数字化时代企业实现数据价值最大化的核心前提,而CDA(Certified Data Analyst)数据分析师作为数据全生命周期的核心 ...
2026-03-05在实验检测、质量控制、科研验证等场景中,“方法验证”是确保检测/分析结果可靠、可复用的核心环节——无论是新开发的检测方法 ...
2026-03-04在数据分析、科研实验、办公统计等场景中,我们常常需要对比两组数据的整体差异——比如两种营销策略的销售额差异、两种实验方案 ...
2026-03-04在数字化转型进入深水区的今天,企业对数据的依赖程度日益加深,而数据治理体系则是企业实现数据规范化、高质量化、价值化的核心 ...
2026-03-04在深度学习,尤其是卷积神经网络(CNN)的实操中,转置卷积(Transposed Convolution)是一个高频应用的操作——它核心用于实现 ...
2026-03-03在日常办公、数据分析、金融理财、科研统计等场景中,我们经常需要计算“平均值”来概括一组数据的整体水平——比如计算月度平均 ...
2026-03-03在数字化转型的浪潮中,数据已成为企业最核心的战略资产,而数据治理则是激活这份资产价值的前提——没有规范、高质量的数据治理 ...
2026-03-03在Excel办公中,数据透视表是汇总、分析繁杂数据的核心工具,我们常常通过它快速得到销售额汇总、人员统计、业绩分析等关键结果 ...
2026-03-02在日常办公和数据分析中,我们常常需要探究两个或多个数据之间的关联关系——比如销售额与广告投入是否正相关、员工出勤率与绩效 ...
2026-03-02在数字化运营中,时间序列数据是CDA(Certified Data Analyst)数据分析师最常接触的数据类型之一——每日的营收、每小时的用户 ...
2026-03-02在日常办公中,数据透视表是Excel、WPS等表格工具中最常用的数据分析利器——它能快速汇总繁杂数据、挖掘数据关联、生成直观报表 ...
2026-02-28有限元法(Finite Element Method, FEM)作为工程数值模拟的核心工具,已广泛应用于机械制造、航空航天、土木工程、生物医学等多 ...
2026-02-28在数字化时代,“以用户为中心”已成为企业运营的核心逻辑,而用户画像则是企业读懂用户、精准服务用户的关键载体。CDA(Certifi ...
2026-02-28在Python面向对象编程(OOP)中,类方法是构建模块化、可复用代码的核心载体,也是实现封装、继承、多态特性的关键工具。无论是 ...
2026-02-27在MySQL数据库优化中,索引是提升查询效率的核心手段—— 面对千万级、亿级数据量,合理创建索引能将查询时间从秒级压缩到毫秒级 ...
2026-02-27