Python adds content to a file and obtains the file information,

Source: Internet
Author: User

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.

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.