os.path等os模块函数
os.pardir #当前目录的父目录
os.path.join(script_dir, os.pardir, os.pardir) #script_dir的祖父目录,例如:script_dir是F:\1\2\3,那么os.path.join(script_dir, os.pardir, os.pardir)是F:\1\2\3\..\..
os.path.abspath(os.path.join(script_dir, os.pardir, os.pardir)) #script_dir的祖父目录,例如:script_dir是F:\1\2\3,那么os.path.abspath(os.path.join(script_dir, os.pardir, os.pardir))是F:\1
os 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作
1.文件操作
build-in函数 open 实现文件创建, 打开, 修改文件的操作
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 = "c:\samples\sample.txt"
replace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena
replace(file, "tjena", "hello")
2. 目录操作
os 模块包含了许多对目录操作的函数
listdir 函数返回给定目录下的所有文件(包括目录)
import os
for file in os.listdir("c:\qtest"):
print file
getdir 获取当前目录
chdir 改变当前路径
cwd = os.getcwd()
print "1", cwd
# go down
os.chdir("c:\qtest")
print "2", os.getcwd()
# go back up
os.chdir(os.pardir)#返回当前目录的父目录
print "3", os.getcwd()
makedirs removedirs 生成和删除目录
makedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除
import os
os.makedirs("c:\\test\\multiple\\levels")
fp = open("c:\\test\\multiple\\levels\\file.txt", "w")
fp.write("inspector praline")
fp.close()
# remove the file
os.remove("c:\\test\\multiple\\levels\\file.txt")
# and all empty directories above it
os.removedirs("c:\\test\\multiple\\levels")
mkdir 和 rmdir只能处理单级目录操作.
若要删除非空目录, 可使用 shutil模块中的rmtree函数
3. 文件属性的操作
import os
import time
file = 'c:\qtest\editor.pyc'
st = os.stat(file)
print "state", file
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
print dir(st)
dump(st)
fp = open(file)
st = os.fstat(fp.fileno())
print "fstat", file
dump(st)
os.path等os模块函数的更多相关文章
- Python:目录和文件的操作模块os.path和OS常用方法
1.目录和文件的操作模块os.path,在使用之前要先导入:import os.path.它主要有以下几个重要的功能函数: #!/user/bin/python #coding= utf-8 impo ...
- Python基本知识 os.path.join与split() 函数
Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串os.path.joi ...
- Python 基础之模块之os os.path 及os与shutil对比
一: os 对系统进行操作 #注:以下操作都在linux环境下操作,且很多运行之前需要做好相关条件import os#(1)system() 在python总执行系统命令#os.system(&quo ...
- Python_os、os.path、os.shutil使用案例
import os import os.path print(os.path.basename('/Users/c2apple/Desktop/彩屏')) #获取路径的最后一个组成部分 os.path ...
- 4、Python文件对象及os、os.path和pickle模块(0530)
文件系统和文件 1.文件系统是OS用于明确磁盘或分区上的文件的方法和数据结构---即在磁盘上组织文件的方法: 文件系统模块:os 2.计算机文件(称文件.电脑档案.档案),是存储在某种长期储存设备或临 ...
- python 文件操作,os.path.walk()的回调函数打印文件名
#coding=utf-8 import osdef find_file(arg,dirname,files): #for i in arg: #print i for file ...
- python中 OS模块中 os.path.join() 函数用法简介
基础用法 os.path.join() 用于拼接文件的路径,可以传入多个待拼接的路径 若各个路径之间不存在 " / ", 则其会自动为各个路径之间增加连接符 " / &q ...
- python os.path 模块
os.path模块用法: 1, os.path.basename() >>> os.path.basename('/share/Public/cmiao')'cmiao' basen ...
- 洗礼灵魂,修炼python(17)--跨平台操作三剑客—os,os.path.sys模块
os 1.作用: 因为客户基本都是使用不同的操作系统,在不同的系统下,要完成一个项目,那必须跨平台操作,而python本来就是一个跨平台的语言,而有了os模块,则不需要在意什么系统.并且os模块是用于 ...
随机推荐
- Maven(4)-利用intellij idea创建maven 多模块项目
本文通过一个例子来介绍利用maven来构建一个多模块的jave项目.开发工具:intellij idea. 一.项目结构 multi-module-project是主工程,里面包含两个模块(Modul ...
- Qt中Qstring,char,int,QByteArray之间到转换(转)
11.各种数据类型的相互转换char * 与 const char *的转换char *ch1="hello11";const char *ch2="hello22&qu ...
- CentOS 下tomcat安装
1. 下载tomcat, http://apache.fayea.com/tomcat/tomcat-8/v8.5.16/bin/apache-tomcat-8.5.16.tar.gz 我下载的是这个 ...
- 数据抽取 CDC
什么是数据抽取 数据抽取是指从源数据源系统抽取目的数据源系统需要的数据.实际应用中,数据源较多采用的是关系数据库. [编辑] 数据抽取的方式 (一) 全量抽取 全量抽取类似于数据迁移或数据复制,它将数 ...
- js基础篇string&&array(应YX同学面试复习要求 - -)
js中的数据类型一共有五个基本数据类型,分别是undefined,null,boolean,number,string. js中的Object类型中包括两大类型:Function类型和array类型. ...
- 在vs2010中编译log4cxx-0.10.0详细方法(从下载、编译、解决错误详细介绍)
在vs2010中编译log4cxx-0.10.0详细方法(从下载.编译.解决错误详细介绍) http://blog.sina.com.cn/s/blog_a459dcf501013tbn.html
- Unable to locate \.nuget\NuGet.exe 问题解决办法之一
问题出现的原因是项目下.nuget文件夹下NuGet.exe文件夹不存在导致的 解决办法: 1.右键编辑NuGet.targets文件 将下载NuGet.exe的配置节点DownloadNuGetEx ...
- IDEA中修改各个部位的字体大小
1.菜单栏 Setting -> Appearance&Behavior -> Appearance ->Override default fonts by (not rec ...
- 【HDU】4352 XHXJ's LIS(数位dp+状压)
题目 传送门:QWQ 分析 数位dp 状压一下现在的$ O(nlogn) $的$ LIS $的二分数组 数据小,所以更新时直接暴力不用二分了. 代码 #include <bits/stdc++. ...
- Oracle中 HWM与数据库性能的探讨
链接:http://www.eygle.com/archives/2011/11/oracle_hwm_tuning.html 本文讨论的是oracle中关于table的HWM的内容,主要包括这样几个 ...