一 常用函数

os模块

os.sep 表示默认的文件路径分隔符,windows为\, linux为/
os.walk(spath):
用来遍历目录下的文件和子目录
os.listdir(dirname):列出dirname下的目录和文件
os.mkdir() :
创建目录
os.makedirs():
创建目录,包含中间级目录
os.remove():删除文件,不能是目录
os.rmdir():删除空目录
os.removedirs(path):删除目录及其子目录
os.rename(src,
dst) :修改文件名
os.renames(old, new) :修改文件或目录名,包含中间级

os.chdir("/tmp") : 更改当前目录
os.chmod( "c:\\test\\buildid.txt", stat.S_IWRITE
) : 去除文件的只读属性

os.path模块

os.path.pathsep 表示默认的路径间的分隔符,windows为;
Linux为:
os.path.isdir(name):判断name是不是一个目录,name不是目录就返回false
os.path.isfile(name):判断name是不是一个文件,不存在name也返回false
os.path.exists(name):判断是否存在文件或目录name
os.path.getsize(name):获得文件大小,如果name是目录返回0L
os.path.getctime(name):获得文件的创建时间

os.path.getmtime(name):获得文件的修改时间

os.path.getatime(name):获得文件的最后访问时间

os.path.isabs(name):测试是否是绝对路径
os.path.abspath(name):获得绝对路径
os.path.normpath(path):规范path字符串形式

os.path.relpath(path,
start='.'):返回路径的相对版本

os.path.split(name):分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)
os.path.splitext():分离文件名与扩展名
os.path.splitdrive():分离驱动名或unc名字
os.path.join(path,name):连接目录与文件名或目录

os.path.basename(path):返回文件名
os.path.dirname(path):返回文件路径

os.path.expanduser("~"):用来获得user的home路径。

shutil模块
shutil.copyfile(src,
dst): 拷贝文件
shutil.copytree(srcDir, dstDir) : 拷贝目录

shutil.rmtree('dir') : 删除非空文件夹

shutil.move('old','new') :修改文件和目录名称

glob模块

匹配文件:glob.glob(r”c:\linuxany\*.py”)

二 实例 (os.walk的遍历过程如下)

 1 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
2
3 import os
4
5 # tree c:\test /f
6 #C:\TEST
7 #│ test.log
8 #│
9 #├─test2
10 #│ test2.log
11 #│
12 #└─test3
13
14 tree = os.walk('C:/test')
15 for directoryItem in tree:
16 directory=directoryItem[0]
17 subDirectories=directoryItem[1]
18 filesInDirectory=directoryItem[2]
19 print('-----------------')
20 print('the directory is :', directory)
21 print('the sub directories are : ', subDirectories)
22 print('the files are :', filesInDirectory)
23
24 #-----------------
25 #the directory is : C:/test
26 #the sub directories are : ['test2', 'test3']
27 #the files are : ['test.log']
28 #-----------------
29 #the directory is : C:/test\test2
30 #the sub directories are : []
31 #the files are : ['test2.log']
32 #-----------------
33 #the directory is : C:/test\test3
34 #the sub directories are : []
35 #the files are : []

转:http://www.cnblogs.com/itech/archive/2009/12/16/1625636.html

Python模块: 文件和目录os+shutil的更多相关文章

  1. Python之文件与目录操作及压缩模块(os、shutil、zipfile、tarfile)

    Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读取或写入 os.path模块 文件路径操作 os模块 文件和目录简单操作 zipfile模 ...

  2. 【转】Python之文件与目录操作(os、zipfile、tarfile、shutil)

    [转]Python之文件与目录操作(os.zipfile.tarfile.shutil) Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读 ...

  3. Python之文件与目录操作(os、zipfile、tarfile、shutil)

    Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读取或写入 os.path模块 文件路径操作 os模块 文件和目录简单操作 zipfile模 ...

  4. Python操作文件和目录

    Python操作文件和目录 读写文件比较简单,有一点特别注意就好了 windows下Python默认打开的文件以gbk解码,而一般我们的文件是utf-8编码的,所以如果文本含有中文,就会出现异常或者乱 ...

  5. python获取文件所在目录

    1.执行的python程序获取自己文件所在目录 import os,sys os.chdir(sys.path[0]); dir_name = os.path.abspath(os.path.join ...

  6. Python::OS 模块 -- 文件和目录操作

    os模块的简介参看 Python::OS 模块 -- 简介 os模块的进程管理 Python::OS 模块 -- 进程管理 os模块的进程参数 Python::OS 模块 -- 进程参数 os模块中包 ...

  7. python 中文件输入输出及os模块对文件系统的操作

    整理了一下python 中文件的输入输出及主要介绍一些os模块中对文件系统的操作. 文件输入输出 1.内建函数open(file_name,文件打开模式,通用换行符支持),打开文件返回文件对象. 2. ...

  8. Python之文件操作:os模块

    Python os 模块提供了一个统一的操作系统接口函数 一.对于系统的操作 1.os.name 当前使用平台 其中 ‘nt’ 是 windows,’posix’ 是linux 或者 unix 2.o ...

  9. python操作文件和目录查看、创建、删除、复制

    python内置了os模块可以直接调用操作系统提供的接口函数,os.name查询的是操作系统,‘nt’表示windows系统 >>> import os >>> o ...

随机推荐

  1. Greys--JVM异常诊断工具

    https://github.com/oldmanpushcart/greys-anatomy/wiki/greys-pdf 一.简介 我们平时在线上或者开发中会遇到各种性能.功能等问题,在运行过程中 ...

  2. 【转】sql递归查询问题

    原文链接地址http://www.cnblogs.com/sweting/archive/2009/06/08/1498483.html 在工作中遇到一个问题,是需要sql递归查询的.不懂,于是到cs ...

  3. elasticsearch5环境搭建

    1.下载ElasticSearch https://www.elastic.co/cn/downloads/elasticsearch#ga-release 因为是windows版本,所以下载zip即 ...

  4. set_magic_quotes_runtime set_magic_quotes_gpc

    set_magic_quotes_runtime(0); 可以修改php.ini中 magic_quotes_runtime boolean的设置 当你的数据中有一些\"'这样的字符要写入到 ...

  5. js获取元素下标

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  6. G - Christmas Play

    Description My kid's kindergarten class is putting up a Christmas play.  (I hope he gets the lead ro ...

  7. WPF Slider滑动条的颜色修改

    效果如下: 鄙人虽然开发WPF有些时间,但之前一直是一些简单Template和Style改改之类的工作,并没有深入研究过.此次为了完成工作,首先也是网上搜了半天,没有找到合适的代码直接拷贝(搜索能力待 ...

  8. WPF 斜角border

    最近看了一些科技感UI设计,其中很多的按钮都不是常见的圆角边,而是斜角边.查了一下,wpf中好像没有现成的斜角border,网上也没搜到现成的,于是自己写了点时间做了一个,写的较简单,有一些bug(主 ...

  9. WPF的xaml中特殊字符表示

    直接看表,描述很清晰 字符 转义字符 备注 & (ampersand) & 这个没什么特别的,几乎所有的地方都需要使用转义字符 > (greater-than character ...

  10. 在ASP.NET MVC应用中开发插件框架(中英对照)

    [原文] Developing a plugin framework in ASP.NET MVC with medium trust [译文] 在ASP.NET MVC应用中开发一个插件框架 I’v ...