您的位置:首页 > 编程语言 > Python开发

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 file copy file