Python模块: 文件和目录os+shutil
一 常用函数
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):返回文件路径
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的更多相关文章
- Python之文件与目录操作及压缩模块(os、shutil、zipfile、tarfile)
Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读取或写入 os.path模块 文件路径操作 os模块 文件和目录简单操作 zipfile模 ...
- 【转】Python之文件与目录操作(os、zipfile、tarfile、shutil)
[转]Python之文件与目录操作(os.zipfile.tarfile.shutil) Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读 ...
- Python之文件与目录操作(os、zipfile、tarfile、shutil)
Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读取或写入 os.path模块 文件路径操作 os模块 文件和目录简单操作 zipfile模 ...
- Python操作文件和目录
Python操作文件和目录 读写文件比较简单,有一点特别注意就好了 windows下Python默认打开的文件以gbk解码,而一般我们的文件是utf-8编码的,所以如果文本含有中文,就会出现异常或者乱 ...
- python获取文件所在目录
1.执行的python程序获取自己文件所在目录 import os,sys os.chdir(sys.path[0]); dir_name = os.path.abspath(os.path.join ...
- Python::OS 模块 -- 文件和目录操作
os模块的简介参看 Python::OS 模块 -- 简介 os模块的进程管理 Python::OS 模块 -- 进程管理 os模块的进程参数 Python::OS 模块 -- 进程参数 os模块中包 ...
- python 中文件输入输出及os模块对文件系统的操作
整理了一下python 中文件的输入输出及主要介绍一些os模块中对文件系统的操作. 文件输入输出 1.内建函数open(file_name,文件打开模式,通用换行符支持),打开文件返回文件对象. 2. ...
- Python之文件操作:os模块
Python os 模块提供了一个统一的操作系统接口函数 一.对于系统的操作 1.os.name 当前使用平台 其中 ‘nt’ 是 windows,’posix’ 是linux 或者 unix 2.o ...
- python操作文件和目录查看、创建、删除、复制
python内置了os模块可以直接调用操作系统提供的接口函数,os.name查询的是操作系统,‘nt’表示windows系统 >>> import os >>> o ...
随机推荐
- hdu 5050 大数
http://acm.hdu.edu.cn/showproblem.php?pid=5050 大数模板最大公约数 信kuangbin,能AC #include <cstdio> #incl ...
- centeros6.8 下安装mysql教程
1.1 安装Mysql 1.1.1 检查 l 检查是否已安装mysql的相关包 [root@localhost ~]# rpm -qa|grep -i mysql 一般情况下,centeros系统中会 ...
- 关于 Keil uVision2 中文注释会显示不完整,字体不正常的问题
在Keil中添加中文注释经常出现这样情况: ,注释文字不正常! 解决方案:Edit---->Option----->选择color&fonts选项卡中的Editor c Files ...
- 修改vsftpd的默认根目录
修改ftp的根目录只要修改/etc/vsftpd/vsftpd.conf文件即可: 加入如下几行: local_root=/var/www/html chroot_local_user=YES ano ...
- sqlserver 自动创建作业执行备份数据库
declare @name varchar(250)set @name='I:\dydb_n\dydb_n'+convert(varchar(50),getdate(),112)+ left(righ ...
- SQL Server nested loop join 效率试验
从很多网页上都看到,SQL Server有三种Join的算法, nested loop join, merge join, hash join. 其中最常用的就是nested loop join. 在 ...
- C# 加特效
public class BulletMove : MonoBehaviour { public float Speed = 5f; public Transform Fx; // Use this ...
- AreaHttpControllerSelector 对 Web Api 实现 Area 路由控制
结合此文章:http://www.cnblogs.com/wuhuacong/p/5828038.html using System; using System.Collections.Concurr ...
- python做数据分析pandas库介绍之DataFrame基本操作
怎样删除list中空字符? 最简单的方法:new_list = [ x for x in li if x != '' ] 这一部分主要学习pandas中基于前面两种数据结构的基本操作. 设有DataF ...
- CentOS修改yum源为阿里云
yum的工具,自动去下载某个yum仓库的 rpm软件包,并且自动搜索软件下载软件依赖,如同pip3,npm等包管理工具 yum载linux中的 yum仓库是 /etc/yum.repos.d 并且在这 ...