
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
数据驱动营销革命:解析数据分析在网络营销中的核心作用 在数字经济蓬勃发展的当下,网络营销已成为企业触达消费者 ...
2025-06-23随机森林模型与 OPLS-DA 的优缺点深度剖析 在数据分析与机器学习领域,随机森林模型与 OPLS-DA(正交偏最小二乘法判 ...
2025-06-23CDA 一级:开启数据分析师职业大门的钥匙 在数字化浪潮席卷全球的今天,数据已成为企业发展和决策的核心驱动力,数据分析师 ...
2025-06-23透视表内计算两个字段乘积的实用指南 在数据处理与分析的过程中,透视表凭借其强大的数据汇总和整理能力,成为了众多数据工 ...
2025-06-20CDA 一级考试备考时长全解析,助你高效备考 CDA(Certified Data Analyst)一级认证考试,作为数据分析师领域的重要资格认证, ...
2025-06-20统计学模型:解锁数据背后的规律与奥秘 在数据驱动决策的时代,统计学模型作为挖掘数据价值的核心工具,发挥着至关重要的作 ...
2025-06-20Logic 模型特征与选择应用:构建项目规划与评估的逻辑框架 在项目管理、政策制定以及社会服务等领域,Logic 模型(逻辑模型 ...
2025-06-19SPSS 中的 Mann-Kendall 检验:数据趋势与突变分析的利器 在数据分析的众多方法中,Mann-Kendall(MK)检验凭借其对数据分 ...
2025-06-19CDA 数据分析能力与 AI 的一体化发展关系:重塑数据驱动未来 在数字化浪潮奔涌的当下,数据已然成为企业乃至整个社会发展进 ...
2025-06-19CDA 干货分享:统计学的应用 在数据驱动业务发展的时代浪潮中,统计学作为数据分析的核心基石,发挥着无可替代的关键作用。 ...
2025-06-18CDA 精益业务数据分析:解锁企业增长新密码 在数字化浪潮席卷全球的当下,数据已然成为企业最具价值的资产之一。如何精准地 ...
2025-06-18CDA 培训:开启数据分析师职业大门的钥匙 在大数据时代,数据分析师已成为各行业竞相争夺的关键人才。CDA(Certified Data ...
2025-06-18CDA 人才招聘市场分析:机遇与挑战并存 在数字化浪潮席卷各行业的当下,数据分析能力成为企业发展的核心竞争力之一,持有 C ...
2025-06-17CDA金融大数据案例分析:驱动行业变革的实践与启示 在金融行业加速数字化转型的当下,大数据技术已成为金融机构提升 ...
2025-06-17CDA干货:SPSS交叉列联表分析规范与应用指南 一、交叉列联表的基本概念 交叉列联表(Cross-tabulation)是一种用于展示两个或多 ...
2025-06-17TMT行业内审内控咨询顾问 1-2万 上班地址:朝阳门北大街8号富华大厦A座9层 岗位描述 1、为客户提供高质量的 ...
2025-06-16一文读懂 CDA 数据分析师证书考试全攻略 在数据行业蓬勃发展的今天,CDA 数据分析师证书成为众多从业者和求职者提升竞争力的重要 ...
2025-06-16数据分析师:数字时代的商业解码者 在数字经济蓬勃发展的今天,数据已成为企业乃至整个社会最宝贵的资产之一。无论是 ...
2025-06-16解锁数据分析师证书:开启数字化职业新篇 在数字化浪潮汹涌的当下,数据已成为驱动企业前行的关键要素。从市场趋势研判、用 ...
2025-06-16CDA 数据分析师证书含金量几何?一文为你讲清楚 在当今数字化时代,数据成为了企业决策和发展的重要依据。数据分析师这一职业 ...
2025-06-13