Operations for local files:
Open ():
#!/usr/bin/env python#--*--coding:utf-8--*--" "Open () gets a handle to the open () parameter description: R read the form open RU to identify different systems of newline W write form open, and clear the original content, the file does not exist in the form of creating the file a append write opened handle action method: Seek pointer offset flush commit update close closed open file" "" "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') forLineinchWfile:Print LinePrint "---------"Wfile= Open ('C:\\users\\lixin\\desktop\\222\\2223.txt','R')PrintWfile.readline ()Print "---------" PrintWfile.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 () mode for opening files:
CSV file operation:
Import= open ('c:\\users\\lixin\\desktop\\222\\222.csv',' RU ' = csv.reader (csvfile) for in csvreader: Print I
Read all files under one path (recursive operation):
#!/usr/bin/env python#--*--coding:utf-8--*--ImportOS fromLoggingImportRoot#the OS reads all the files under one pathdefdirtree (path): forRoot,dirs,filesinchOs.walk (path): forFileNameinchFiles:PrintOs.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:\\u sers\\lixin\\desktop\\222 ')" "
python--file Operations