Python adds content to a file and obtains the file information,
This example describes how to add content to a file and obtain file information in Python. Share it with you for your reference. The specific analysis is as follows:
When adding content to a file, it is easy to add content to the end of the file:
File = open (filename, 'A') file. write ('hello') file. close ()
After opening a file in 'A' mode, the Pointer Points to the end of the file by default, even if you:
File. seek (0) file. write ('World ')
The 'World' string will still be added at the end of the file, rather than the starting position you want.
What should I do if I want to add something to the file header? I don't want to read everything in it. Can I write it in?
Fortunately, we saw the 'R + 'mode (never used before)
File = open (filename, 'r + ') file. tell () # 0Lfile. write ('begin') file. close ()
Open the file and see if you can ;)
File modification time:
>>> T = OS. path. getmtime (path) >>> t1190626843 >>> type (t) <type 'int' >>>> OS. stat (path) [8] 1190626843
Get the file size:
>>> OS. stat (path) [6] 2808L >>> OS. path. getsize (path) 2808L
I hope this article will help you with Python programming.