Python basic tutorial _ Study Notes 21: files and materials

Source: Internet
Author: User
Tags pprint

Python basic tutorial _ Study Notes 21: files and materials
Open files and materials

The open function is used to open a file. The syntax is as follows:

Open ([name [, mode [, buffering])

The open function uses a file name as a unique mandatory parameter and returns a file object. The mode and buffering parameters are optional.

>>> F = open (r 'd: \ software (x86) \ Python27 \ README.txt ')

If the file does not exist, an error occurs:

>>> F = open (r 'd: \ software (x86) \ Python27 \ README_unknown.txt ')

Traceback (most recent call last ):

File" ", Line 1, in

F = open (r 'd: \ software (x86) \ Python27 \ README_unknown.txt ')

IOError: [Errno 2]

No such file or directory: 'd: \ software (x86) \ Python27 \ README_unknown.txt'

File Mode

If the open function only includes one file name parameter, you can obtain the object that can read the file content.

To write content to a file, you must provide a mode parameter for explicit declaration. The mode parameter in the open function has only a few values.

Value description

'R' read mode

'W' write mode

'A' append Mode

'B' binary mode (can be added to other modes)

'+' Read/write mode (can be added to other modes)

The effect of specifying the Read mode and the mode parameters is the same. You can use the write mode to write content to a file.

The '+' parameter can be used in any other mode, specifying that both read and write are allowed. For example, 'r + 'can be used to open a file for reading and writing.

'B' mode changes the method for processing files. In general, python assumes that it processes text files (including characters ). Normally, this will not be a problem, but if you are dealing with some other types of files (binary files), such as audio clips or images, you should add 'B' to the mode parameter '. The 'rb' parameter can be used to read a binary file.

Note: By using the U parameter in the mode parameter, you can use the common linefeed mode when opening a file. In this mode, all linefeeds/strings (\ r \ n, \ r or \ n) are converted to \ n without considering the running platform.

Buffer

The 3rd parameters of the open function control the File Buffer.

If the parameter is 0 or False, I/O is unbuffered (all read/write operations are directly targeted at the hard disk );

If the parameter is 1 or True, I/O is buffered (python uses memory instead of hard disk to make the program faster and updates the data on the hard disk only when flush or close );

A number greater than 1 indicates the buffer size (in bytes ),

-1 (or other negative values) indicates the default buffer size.

Basic file Method

Class object is an object that supports some file methods. More importantly, it supports the read or write methods, or both.

Read and Write

The most important capability of a file (or stream) is to provide or accept data. If there is a class object named f, you can use the f. write method and the f. read method (in string form) to write and read data.

Each time f. write (string) is called, the provided parameter string is appended to the end of an existing part of the file.

>>> F = open ('d: \ software (x86) \ Python27 \ temp \ temp.txt ', 'w ')

>>> F. write ('hello ,')

>>> F. write ('signjing! ')

>>> F. close ()

Call the close function when you complete operations on a file.

>>> F = open ('d: \ software (x86) \ Python27 \ temp \ temp.txt ', 'R ')

Reading is very easy. Just remember to tell the number of characters (bytes) to be read.

>>> F. read ()

'Hello, signjing! '

When the number of characters to be read is not provided, the read function reads the remaining files.

>>> F. read ()

''

>>> F. close ()

When opening a file in 'W' mode, even if the file is not written, the file is cleared first.

Tubular output

Unix shell, you can use a pipeline to run multiple other commands after a command.

GNU bash is also available in windows. For more information, see cygwin;

On Mac OS X, shell files can be used through terminals.

$ Cat somescript. py

# File: some script. py

Import sys

Text = sys. stdin. read ()

Word = text. split ()

Wordcount = len (word)

Print "Wordcount:", wordcount

Print [w for w in word]

$ Cat somescript. py | python somescript. py

Wordcount: 17

['# File:', 'some', 'script. py', 'import', 'sys ', 'text = sys. stdin. read () ', 'word = text. split () ', 'wordcount = len (word)', 'print ',' "wordcount: ','", Wordcount ', 'print', '[W ', 'For ', 'w', 'in', 'word]']

Read/write rows

Generally, reading files one by character is normal, and reading files row by row is acceptable.

You can also use file. readline to read a single line (from the current position until a line break appears, also read this line break ).

No parameter is used or a non-negative integer is used as the maximum value of characters (or bytes) that can be read by readline.

The readlines method can read all rows in a file and return them as a list.

The writelines method is opposite to readlines: A list of strings that are passed to it (in fact, any sequence or iteratable object). It writes all strings to a file (or stream ).

Note: The program does not add new lines. You must add new lines by yourself. There is no writeline method, because it can be replaced by write.

Close file

Remember to close the file by using the close method.

Generally, a file object is automatically closed after exiting the program (or before exiting). Although it is not very important to close the file, closing the file is harmless, you can avoid unnecessary modifications in some operating systems or settings. This will also avoid using the quota of files opened in the system.

The written files should always be closed, because python may cache data (temporarily store data somewhere for efficiency consideration). If the program crashes for some reason, then the data will not be written into the file at all. To ensure security, close the file after it is used.

If you want to ensure that the file is closed, use the try/finally statement and call the close method in the finally clause.

In fact, there are statements specifically designed for this situation, that is, with statements:

With open(“somefile.txt ") as somefile:

Do_something (somefile)

The with statement can open the file and assign it to the variable. After that, you can write data to the file in the statement body or perform other operations. The file is automatically closed after the statement ends, even if it ends due to an exception.

How to use basic files

Read (n)

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ')

>>> F. read (7)

'# File :'

>>> F. read (4)

'Some'

>>> F. close ()

Read ()

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ')

>>> Print f. read ()

# File: some script. py

Import sys

Text = sys. stdin. read ()

Word = text. split ()

Wordcount = len (word)

Print "Wordcount:", wordcount

Print [w for w in word]

>>> F. close ()

Readline ()

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ')

>>> For I in range (3 ):

... Print str (I) + ':' + f. readline ()

...

0: # File: some script. py

1:

2: import sys

Readlines ()

>>> Import pprint

>>> Pprint. pprint (open (R'/home/ggz2/magiccube/mysh/pys/somescript. py '). readlines ())

['# File: some script. py \ n ',

'\ N ',

'Import sys \ n ',

'Text = sys. stdin. read () \ n ',

'Word = text. split () \ n ',

'Wordcount = len (word) \ n ',

'Print "Wordcount:", wordcount \ n ',

'Print [w for w in word] \ n']

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ')

>>> F. readlines ()

['# File: some script. py \ n', '\ n', 'import sys \ n', 'text = sys. stdin. read () \ n', 'word = text. split () \ n', 'wordcount = len (word) \ n', 'print "wordcount:", Wordcount \ n ', 'print [w for w in word] \ n']

Write (string)

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ', 'w ')

>>> F. write ("print 'hello, \ nSignjing! '")

>>> F. close ()

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ')

>>> F. readlines ()

["Print 'hello, \ n", "Signjing! '"]

Writelines (list)

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ')

>>> Lines = f. readlines ()

>>> F. close ()

>>> Lines [1] = 'isn \'t a \ N'

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ', 'w ')

>>> F. writelines (lines)

>>> F. close ()

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ')

>>> F. readlines ()

["Print 'hello, \ n", "isn' t a \ n"]

>>> F. close ()

Iteratively process File Content in bytes

The most common method to iterate the file content is to use the read method in the while loop: To cycle each character;

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ')

>>> While True:

... Char = f. read (1)

... If not char:

... Break

... Print char

...

P

R

I

N

T

'

H

E

L

L

O

,

I

S

N

'

T

A

>>> F. close ()

Process by line

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ')

>>> While True:

... Line = f. readline ()

... If not line:

... Break

... Print line

...

Print 'hello,

Isn' t

>>> F. close ()

Read all content

If the file is not large, you can use the read method without parameters to read the entire file at a time (read the entire file as a string ), you can also use the readlines method (read the file into a string list, and each string in the list is a line ).

Note: reading the file content into a string or reading the list is useful in other cases. For example, after reading a string, you can use a regular expression or store the row list in some data structures for future use.

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ')

>>> Print [char for char in f. read ()]

['P', 'R', 'I', 'n', 't', ''," '", 'h', 'E', 'l ', 'l', 'O', ',' \ n', 'I, s', 'n', "'",'t ', '', 'A', '\ n']

>>> F. close ()

>>> F = open (R'/home/ggz2/magiccube/mysh/pys/somescript. py ')

>>> Print [line for line in f. readlines ()]

["Print 'hello, \ n", "isn' t a \ n"]

>>> F. close ()

Implement lazy row iteration using fileinput

When performing iterative operations on a very large file, readlines occupies too much memory. In this case, you can use the while + readline method instead.

>>> Import fileinput

>>> Print [line for line in fileinput. input ('/home/ggz2/magiccube/mysh/pys/somescript. py')]

["Print 'hello, \ n", "isn' t a \ n"]

File iterator

File objects can be iterated, which means they can be used directly in the for loop to iterate on them.

As long as the content is not written to the file, it is okay not to close the file.

>>> F = open ('/home/ggz2/magiccube/mysh/pys/somescript. py', 'w ')

>>> F. write ("1st: 1 \ n ")

>>> F. write ("2nd: 2 \ n ")

>>> F. write ("3rd: 3 \ n ")

>>> F. close ()

>>> Lines = list (open ('/home/ggz2/magiccube/mysh/pys/somescript. py '))

>>> Lines

['1st: 1 \ n', '2nd: 2 \ n', '3rd: 3 \ n']

>>> Lines [2]

'3rd: 3 \ N'

>>> Lines [1]

'2nd: 2 \ N'

>>> Lines [0]

'1st: 1 \ N'

Using sequences to unpack an open file and put each row into a single variable is very practical, because it generally does not know how many lines the file has, but it demonstrates the "iteration" of the file object ".

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.