Python notes files

Source: Internet
Author: User

1, File open and create#file () Used for file management (name, mode [, Buffering]]) name is filename, present-open; not present-create mode for open mode, R, R+, W, w+, A, A +, B, u, etc.
2, read the file
#open () reads the file Data=open (' file_name ') #打开一个文件print (Data.redline (), end= ") #用readline () reads a data row Data.seek (0) # Go back to the beginning of the file for the Dataprint (line, end= ") data.close () #关闭文件

How to read a file # #) read by line
f = open ("file.txt") while True:line = F.readline () if Line:print lineelse:break;f.close ()

#2) multi-line Read
f = open (' file.txt ') lines=f.readlines () #readlines () the entire contents of F are saved for the line in the Linesprint line

#3) reads read () once
f = open (' file.txt ') content = F.read () print contentf.close ()


3, File Write
#write (), Writelines () write Files f = file (' file.txt ', ' w+ ') #w + read and write mode open, first delete original, then write new content line = ["Hello \ n", "World \"]f.writelines ( Li) f.close () #追加新内容f = file (' Hello.txt ', ' A + ') content = "Goodbye" f.write (content) F.close ()


4, file Deletion # requires the use of OS module and Os.path Module #os module's open () and file () function and Python built-in usage differs
Import osfile (' Hello.txt ', ' W ') If Os.path.exists (' Hello.txt ') os.remove (' Hello.txt ')


5, File copy
#用read () and write () implementation copy src = file ("Hello.txt", "r") DST = File ("Hello2.txt", "W") Dst.write (Src.read ()) Src.close () Dst.close () #shutil模块 copyfile () function #copyfile (src, DST) # File copy shutil.copyfile ("Hello.txt", "Hello2.txt") #move (SRC, DST) #文件剪切, Move shutil.move ("Hello.txt", "... /") #移动到父目录下shutil. Move (" Hello2.txt "," Hello3.txt ") #移动 + rename


6, File Rename
The rename () function of the #修改文件名 OS module import osos.rename ("Hello.txt", "Hi.txt") #修改后缀名import osfiles = Os.listdir (".") for filename in files:pos = Filename.find (".") If filename[pos+1:] = = "html": newname = filename[:p os+1] + "JSP" os.rename (filename, newname)


7, File content search
#文件查找import ref1 = File ("Hello.txt", "r") Count = 0for s in f1.readlines (): Li = Re.findall ("Hello", s) #findall () query variable s, find results Save to Li If Len (li) > 0:count = Count + li.count ("Hello") print count, "Hello" f1.close () #文件替换f1 = File ("Hello.txt", "R") F2 = File ("Hello2.txt", "W") for S in F1.readlines (): F2.write (S.replace ("Hello", "HI")) f1.close () F2.close ()


8, file comparison #difflib module for sequence, file comparison
9, configuration file access #python standard library Configparser module term resolution profile
10, files and streams
#sys模块中提供了stdin, stdout,stderr three basic Stream object Import Sys#stdin represents the standard input of the stream Sys.stdin = open ("Hello.txt", "R") #读取文件for line in Sys.stdin.readlines () print line#stdout redirect output, save output to file Sys.stdout = open (r "./hello.txt", "a") print "Goodbye" Sys.stdout.close ()



Python notes files

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.