The examples in this paper describe how Python implements adding content to a file and getting file information. Share to everyone for your reference. The specific analysis is as follows:
Often encountered when adding content to the file, if just added at the end of the file, it is relatively simple:
FILE = open (filename, ' a ') file.write (' Hello ') file.close ()
When you open a file using ' A ' mode, the pointer defaults to the end of the file, even if you:
File.seek (0) File.write (' World ')
The string ' World ' will still be added at the end of the file, not where you want it to start.
And I met the demand is to add something in the file header Ah, how to do it? I'm not going to read all the stuff in there, can you write it?
Fortunately see the ' r+ ' this pattern (never used before)
FILE = open (filename, ' r+ ') File.tell () #0Lfile. Write (' Begin ') File.close ()
Open the file to see if it is possible;)
Get the file modification time:
>>> t = os.path.getmtime (path) >>> t1190626843>>> type (t)
>>> Os.stat (path) [8]1190626843
Get the file size:
>>> os.stat (path) [6]2808l>>> os.path.getsize (path) 2808L
Hopefully this article will help you with Python programming.