本地文件的操做:python
open() :spa
#!/usr/bin/env python #--*--coding:utf-8--*-- ''' open()獲取句柄 open()參數說明: r讀的形式打開 rU識別不一樣系統的換行 w寫的形式打開,並清除原有內容,文件不存在就建立該文件 a追加寫的形式打開 句柄操做方法: seek指針的偏移 flush提交更新 close關閉打開的文件 ''' ''' rfile = open('C:\\Users\\lixin\\Desktop\\222\\2223.txt','rU') print(rfile.read()) wfile = open('C:\\Users\\lixin\\Desktop\\222\\2223.txt','w') wfile.write("11111") wfile = open('C:\\Users\\lixin\\Desktop\\222\\2223.txt','a') wfile.write("5555\n444\n") ''' wfile = open('C:\\Users\\lixin\\Desktop\\222\\2223.txt','r+') wfile.seek(0,0) wfile.write("!!!!!") wfile.flush() wfile.close() wfile = open('C:\\Users\\lixin\\Desktop\\222\\2223.txt','r') for line in wfile: print line print "---------" wfile = open('C:\\Users\\lixin\\Desktop\\222\\2223.txt','r') print wfile.readline() print "---------" print wfile.readlines() print "---------" wfile1 = open('C:\\Users\\lixin\\Desktop\\222\\2223.txt','r+') wfile2 = open('C:\\Users\\lixin\\Desktop\\222\\2224.txt','w') wfile2.write(wfile1.read()) ##fobj.close()
open()打開文件的模式:指針
csv文件操做:code
import csv csvfile = open('C:\\Users\\lixin\\Desktop\\222\\222.csv','rU') csvReader = csv.reader(csvfile) for i in csvReader: print i
讀取一個路徑下的全部文件(遞歸操做):blog
#!/usr/bin/env python #--*--coding:utf-8--*-- import os from logging import root #os讀取一個路徑下的全部文件 def dirTree(path): for root,dirs,files in os.walk(path): for filename in files: print os.path.join(root,filename) dirTree('C:\\Users\\lixin\\Desktop\\222') ''' #os讀取一個路徑下的全部文件 def dirTree(path): li = os.listdir(path) for obj in li: #filepath = path + '\\' +obj filepath = os.path.join(path,obj) if os.path.isdir(filepath): #print '*******'+filepath+'' dirTree(filepath) else: print filepath #刪除 #os.remove(filepath) dirTree('C:\\Users\\lixin\\Desktop\\222') '''