Open file, current directory Help (file) Method. Close () Close the file. File can no longer read and write after closing: Flush () refreshes the internal buffer of the file, directly writes the data of the internal buffers to the file immediately, instead of passively waiting for the output buffer to be written to: Fileno () Returns a file descriptor for an integer type (descriptor FD integer) Can be used in some of the underlying operations, such as the Read method of the OS module: Isatty () If the file is connected to an end device returns TRUE, otherwise False: Next () & nbsp returns the next line in the file. read ([size]) reads the specified number of bytes from the file, if it is not given or is negative read all. ReadLine ([size]) reads the entire line, including the "\ n" character: ReadLines ([Sizehint]) &NBsp; reads all rows and returns a list, and if given sizeint>0, returns a row with a sum of approximately sizeint bytes, the actual read value may be larger than sizhint because the buffer needs to be populated. .seek (offset[, whence]) Set the current location of the file. Tell () Returns the current location of the file. Truncate ([size]) truncate file, truncated bytes specified by size, default to the current file location: Write ( STR) Writes a string to the file without a return value: Writelines (sequence) writes a list of sequence strings to the file and adds a newline character to each line if it needs to be wrapped. Data type descriptionFile.closed returns True if the file has been closed, otherwise false is returned. file.encoding file Encoding File.errorsfile.mode returns the access mode of the file being opened. File.name returns the name of the file. File.newlines File.softspace If the print output must be followed by a space character, false is returned. Otherwise, returns True.File WriteFile (' Test.txt ', ' W ') r read-only mode, the files are present only, the default mode RB opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode r+ open a file for read-write. The file pointer will be placed at the beginning of the file rb+ open a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file W write, the newly created Test.txt file,Files are overwritten when there is a file,A append + read and write
>>> file ('Test.txt','W')<open file'Test.txt', mode'W'At 0x0000000002e24150>##文件存在内存中>>> f = File ('Test.txt','W')>>> F.write ("First test")##写入内容>>> F.write ("AAA") >>>f.flush ()##刷新写入, save on hard drive>>>f.close ()##文件退出, save on hard driveCannot f.write () after exiting, if open in W mode again
f = File ('test.txt','w') f.write ("add Test2") f.close () = file ('test.txt',' a ')
Write a list
f = open ("test.txt""wb") f.writelines ([' First test\n ' ' 123\n ' ' AAA ' ]) f.close ()
open file, with file properties Open
#!/usr/bin/python#-*-coding:utf-8-*- #Open a filef = open ("F.txt","WB")Print "file name:", F.namePrint "is closed:", f.closedPrint "access mode:", F.modePrint "whether to force a space at the end:", F.softspace Condecs Avoid formatting problems open file garbledRecommended
Import codecs>>> f = codecs.open ('test.txt','a', ' Utf-8 ' )>>> f.write ('test')>>> f.close ()
Read the fileF.readline () Read line
>>>f = File ('Test.txt')## #不带模式, default is R read-only mode>>>F.read () reads everything, prints all content f.read () executes f.readline () and displays a null value, because the pointer defaults to the end of the content after F.read () ,>>>f.tell ()##可以查看指针位置,>>>f.seek (0)##指针跳到0个字符, first, specify to jump to that position>>>f.readline ()## #读出所有内容, print the first line, then print the second line>>>f.seek (0)>>>f.readlines ()##读取所有行并返回列表>>>['First test\n','123\n','AAA']F.xreadlines () # #对于很大的文件时使用, read single line, fast account for small memoryWith ... as syntax,enumerate () use
import Codecswith codecs open ( " test.txt , " rb '
With ... asAvoid forgetting f.close (), and can avoid garbled filesLinecache fetching a line of content
Import= linecache.getline (test.txt,4)print(count)
File content SubstitutionFor the Fileinput.input ("filepath", inplace=1): line = Line.replace ("OldText", "NewText") print line, # # #不写 , the file may be emptied inplace=0 default = 0 only print modified, =1 modify the file backup= '. bak ' before you back up the source file, add the. bak suffix
Python file processing