Python学习笔记(九)
2016-04-19 15:48
621 查看
Python File Operate
Python 对文件的操作1、fp = open('filename','r') #以读的方式打开文件
w 以写方式打开,
a 以追加模式打开 (从 EOF 开始, 必要时创建新文件)
r+ 以读写模式打开
w+ 以读写模式打开
a+ 以读写模式打开
rb 以二进制读模式打开
wb 以二进制写模式打开
ab 以二进制追加模式打开
rb+ 以二进制读写模式打开
wb+ 以二进制读写模式打开
ab+ 以二进制读写模式打开
2、os.path.isdir()函数判断某一路径是否为目录,是的话,返回True
os.path.isfile()函数判断某一路径是否为文件,是的话,返回True
os.listdir()函数获得指定目录中的内容 ,以一个列表的形式进行返回['xxx.ini','xxxs.jpg',]
os.getcwd()获得当前目录
例子:将指定目录下的所有文件复制到另一个目录下:
import os,shutil
def list_files_to_file(path,filename):
files = []
if not os.path.isdir(path):
return False
else:
files = os.listdir(path) #list --no problems
#print files
for i in files:
#i+=1
f = i.split('.')
ext = f[1]
#if ext != 'ini':
shutil.copy(path+'\\'+i,filename)
print ext
#else:
#continue
return True
调用:
if list_files_to_file('C:\Users\Administrator\Pictures','E:\Python\workspace'):
print 'OK'
result:
target dictionary: 显示复制过去的文件
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法