Python built-in function 4-file ()

Source: Internet
Author: User
Tags readline truncated

Help on the class file in module __builtin__:


class file (object)

| File (name[, mode[, buffering]), file object

|

|  Open a file. The mode can is ' R ', ' W ' or ' A ' for reading (default),

|  Writing or appending. The file would be created if it doesn ' t exist

| When opened for writing or appending; It'll be truncated when

|  Opened for writing. Add a ' B ' to the mode for binary files.

| Add a ' + ' to the mode to allow simultaneous reading and writing.

| If The buffering argument is given, 0 means unbuffered, 1 means line

|  Buffered, and larger numbers specify the buffer size. The preferred

| To open a file was with the Builtin open () function.

| Add a ' U ' to mode-open the file for input with universal newline

|  Support. Any line ending in the input file would be seen as a ' \ n '

|  In Python. Also, a file so opened gains the attribute ' newlines ';

| The value for this attribute is one of None (no newline read yet),

| ' \ r ', ' \ n ', ' \ r \ n ' or a tuple containing all the newline types seen.

|

| ' U ' cannot is combined with ' w ' or ' + ' mode.

|

| Methods defined here:

|

| __delattr__ (...)

| x.__delattr__ (' name ') <==> del x.name

|

| __enter__ (...)

| __ENTER__ (), self.

|

| __exit__ (...)

|  __exit__ (*excinfo), None. Closes the file.

|

| __getattribute__ (...)

| x.__getattribute__ (' name ') <==> x.name

|

| __init__ (...)

| X.__init__ (...) initializes x; See Help (Type (x)) for signature

|

| __iter__ (...)

| X.__ITER__ () <==> iter (x)

|

| __repr__ (...)

| x.__repr__ () <==> repr (x)

|

| __setattr__ (...)

| x.__setattr__ (' name ', value) <==> X.name = value

|

| Close (...)

|  Close (), None or (perhaps) an integer. Close the file.

|

|  Sets data attribute. Closed to True. A closed file cannot is used for

|  Further I/O operations. Close () May is called more than once without

|  Error. Some Kinds of File objects (for example, opened by Popen ())

| May return an exit status upon closing.

|

| Fileno (...)

| Fileno (), integer "File descriptor".

|

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

|

| Flush (...)

|  Flush (), None. Flush the internal I/O buffer.

|

| Isatty (...)

|  Isatty () True or false. True if the file is connected to a TTY device.

|

| Next (...)

| X.next (), the next value, or raise stopiteration

|

| Read (...)

| Read ([size]), read at most size bytes, returned as a string.

|

| If the size argument is negative or omitted, read until EOF is reached.

| Notice that at non-blocking mode, less data than what is requested

| May is returned, even if no size parameter was given.

|

| Readinto (...)

|  Readinto (), undocumented. Don ' t use this; It may go away.

|

| ReadLine (...)

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

|

|  Retain newline. A non-negative size argument limits the maximum

| Number of bytes to return (a incomplete line is returned then).

| Return an empty string at EOF.

|

| 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.

|

| Seek (...)

|  Seek (offset[, whence]), None. Move to new file position.

|

|  Argument offset is a byte count. Optional argument whence defaults to

| 0 (offset from start of file, offset should is >= 0); Other values is 1

| (move relative to current position, positive or negative), and 2 (move

| Relative to end of file, usually negative, although many platforms allow

|  Seeking beyond the end of a file). If the file is opened in text mode,

|  Only offsets returned by tell () is legal. Use of other offsets causes

| Undefined behavior.

| Note that not all file objects is seekable.

|

| Tell (...)

| Tell ()-current file position, an integer (may be a long integer).

|

| Truncate (...)

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

|

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

|

| Write (...)

|  Write (str), None. Write string str to file.

|

| Note that due to buffering, flush () or close () is needed before

| The file on disk reflects the data written.

|

| 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.

|

| Xreadlines (...)

| Xreadlines (), returns self.

|

| For backward compatibility. File objects now include the performance

| Optimizations previously implemented in the Xreadlines module.

|

| ----------------------------------------------------------------------

| Data descriptors defined here:

|

| Closed

| True If the file is closed

|

| Encoding

| File encoding

|

| Errors

| Unicode Error Handler

|

| Mode

| File mode (' R ', ' U ', ' w ', ' a ', possibly with ' B ' or ' + ' added)

|

| Name

| File name

|

| Newlines

| End-of-line convention used in the This file

|

| Softspace

| Flag indicating a space needs to be printed; Used by print

|

| ----------------------------------------------------------------------

| Data and other attributes defined here:

|

| __new__ = <built-in method __new__ of type object>

| T.__NEW__ (S, ...)-A new object with type S, a subtype of T

File (name[, mode[, Buffering])

Constructor function for the file type, described further in section file Objects. The constructor ' s arguments is the same as those of the open () built-in function described below.


When opening a file, it's preferable to use open () instead of invoking this constructor directly. File is more suited to type testing (for example, writing isinstance (f, file)).


English Description:

read out the contents of a file.

The file () function is a new function in 2.2, which, like the open () function, is equivalent to the alias of Open (), but is more intuitive than open ().


lines = file (filename). ReadLines ()

For line in lines:

Print Line

Ordinal method and Description

1file.close ()

Close the file. The file can no longer read and write after closing.

2file.flush ()

Refreshes the file internal buffer, directly writes the internal buffer data immediately to the file, rather than passively waits for the output buffer to write.

3file.fileno ()

Returns an integer that is a file descriptor (Descriptor FD Integer) that can be used in some of the underlying operations, such as the Read method of the OS module.

4file.isatty ()

Returns True if the file is connected to an end device, otherwise False is returned.

5file.next ()

Returns the next line of the file.

6file.read ([size])

Reads the specified number of bytes from the file, if none is given or is negative.

7file.readline ([size])

Reads the entire line, including the "\ n" character.

8file.readlines ([Sizehint])

Reads all rows and returns a list, and if given sizeint>0, returns a row with a sum of approximately sizeint bytes, the actual read value may be larger than sizhint because the buffer needs to be populated.

9file.seek (offset[, whence])

Set the current location of the file

10file.tell ()

Returns the current location of the file.

11file.truncate ([size])

Intercepts the file, the truncated byte is specified by size, and the default is the current file location.

12file.write (str)

Writes a string to the file without a return value.

13file.writelines (Sequence)

Writes a list of sequence strings to the file and adds a newline character to each line if a line break is required.


This article is from the "Big Cloud Technology" blog, please be sure to keep this source http://hdlptz.blog.51cto.com/12553181/1899784

Python built-in function 4-file ()

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.