Python File Operate
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: 显示复制过去的文件