Article From:https://www.cnblogs.com/lixiang1013/p/9216294.html

1)Catalog

Manual block open+close:OpenCloseAutomatically block with, automatically shut down#1 common operations:W, R, a, BWriteReadLineReadlinesSeek settings read from that locationTGetting the file to read the location now, ellReadRelated modulesFnmatchGlobPickleStringIOShelve

2)File operation

f = open('File name or something like file, file open mode)F is a file object or pointer for reading and writing operations.F.closeThree modesW, write, writeR, read, readA, append, additional contentBeExample 1: write files, open files in W mode, and do not exist to create files.F = open ('111.txt','w')'111.txt'opens files in W mode, and does not exist to create files.Write string to f.write ('111'* 8)F.closE ()The example 2:with opens and the file will automatically shut down.With open ('111-1.txt','w') as f:# file will be automatically closed.F.write ('111')Example 3: after the name of the fileR mode defaultR schema default after with open ('111.txt') as f:Data = f.read ()) read all the contents and save it to a variable. There is no reading error.PRint (data)Example 4: read a lineWith open ('111.txt') as f:Print (f.readline ()) read a lineExample 5:'a'several lines, add to the source fileWith open ('111.txt','a') as f:,'a''a' several lines, appended to the source fileF.write ('111111\n')F.write ('111111\n')IndicationExample 6:readlines reading listWith open ('111.txt') as f:Print (f.readlines ())#['11111\n','111111\n', '\'N','111111\n','111111']7:tell seekF = open ('de8ug.txt')Print (f.tell ())Print (f.readline ())Print (f.tell ())Print (f.readline ())Print (f.tell ())F.seek (0)Print (f.tell ())Print (f.readline ())F.Seek (0)Print (f.readline ())- -- - -- - ---ZeroEleven billion one hundred and eleven million one hundred and eleven thousand one hundred and elevenTenOne hundred and eleven thousand one hundred and elevenTwenty-two billion two hundred and twenty-two million two hundred and twenty-two thousand two hundred and twenty-twoTwentyZeroEleven billion one hundred and eleven million one hundred and eleven thousand one hundred and elevenZeroEleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven

3)Related modules

1)fnmatch

# fnmatchFile matching the corresponding suffix nameImport fnmatchFor F in os.listdir ('test'):If fnmatch.fnmatch (F,'*.txt'): support for regular expressionsPrint (f)Elif fnmatch.fnmatch (F,'*.pdf'):Print ('find pdf', f)Fnmatch matches the corresponding postA named fileImport fnmatchFor F in os.listdir ('test'):If fnmatch.fnmatch (F, '?.txt'): regular,? Match a characterPrint (f)Elif fnmatch.fnmatch (F, '?.pdf'): regular,? Match a characterPrint ('find pdf', f)2) gLobSimply match a certain name rule fileImport globFor F in glob.glob ('test/[0-9].doc'):Print (f)3) StringIOVirtual files,Temporary files do not need to save files to disk.Import IOOutput = io.StringIO ()Output.write ('second line code \n' ")Print ('try print to file:', filE=output)Take out the contentContents = output.getvalue ()Print (contents)Close the file and clean up the cacheOutput.close ()
Link of this Article: Python base — file operation

Leave a Reply

Your email address will not be published. Required fields are marked *