Python File method

Source: Internet
Author: User

#!/usr/bin/env python

# *_* Coding=utf-8 *_*


"""

Desc: File Method



#############################

File.read () #read ([size]), read at the most size bytes, returned as a string.

File.readline () ReadLine ([size]), next line from the file, as a string.

File.readlines ()

ReadLines ([size]), List of strings, each a line from the file.

Call ReadLine () repeatedly and return a list of the lines so read.

The optional size argument, if given, is a approximate bound on the

Total number of bytes in the lines returned.

File.xreadlines () Xreadlines (), returns self.

File.write () write (str), None. Write string str to file.

File.writelines ()

Writelines (sequence_of_strings), None. Write the strings to the file.

Note that newlines is not added. The sequence can is any Iterable object

producing strings. This is equivalent to calling write () for each string.


File.truncate ()

Truncate ([size]), None. Truncate the file to in the most size bytes.

Size defaults to the current file position, as returned by Tell ().

File.seek ()

The Seek (offset [, whence]) method changes the position of the current file. The offset variable represents the number of bytes to move. The from variable specifies the reference position at which to begin moving bytes.

If from is set to 0, this means that the beginning of the file is used as the reference location for moving bytes. If set to 1, the current position is used as the reference location. If it is set to 2, then the end of the file will be used as the reference location.

File.flush ()

File.tell () Current file position, an integer (could be a long integer).

File.next ()

File.close ()



########### #不常用的方法 #############

File.closed

File.mode

File.name

File.isatty () # True or False. True if the file is connected to a TTY device.

File.readinto () #不用用这个

File.encoding # not used

File.errors # not used

File.newline # not used

File.softspace

File.fileno ()

Fileno (), integer "File descriptor".

This is needed for Lower-level file interfaces, such Os.read ().

###############################

version:1.0

"""


Fo= Open ("E:/temp.txt", ' R ')

Print "File name:", Fo.name

Print "is closed:", fo.closed

Print "Access mode:", Fo.mode

Print "whether to force a space at the end:", Fo.softspace






Python File method

Related Article

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.