python--file Operations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.