Python file input and output

Source: Internet
Author: User
Tags create directory

1.1 File objects

The file is just a sequential sequence of bytes. Data transfers often use byte streams, regardless of whether the byte stream consists of a single or large chunk of data.
1.2 Files built-in functions open () and file ()
The basic syntax for the built-in function open () is:
File_object=open (file_name,access_mode= ' R ', Buffering=-1)

File_name is a string containing the name of the file to be opened, which can be a relative path or an absolute path.

The Access_mode optional variable is also a string that represents the mode in which the file opens.

' R ' stands for: read;

' W ' stands for: write;

' A ' means: append;

' U ' stands for: Universal line break support

Files opened using ' R ' or ' U ' mode must already exist

Files opened with ' W ' mode are emptied first if they exist, and then (re) created

Files opened in ' A ' mode are prepared for appending data, and all written data is appended to the end of the file. Even if you seek elsewhere. If the file does not exist, it will be created automatically.

Include the use of ' B '-if you choose to use it. If no access_mode is given, it will automatically take the default value of ' R '.

The buffering optional parameter is used to indicate the buffering method used to access the file:

0 means no buffering

1 means only one row of data is buffered

Any other value greater than 1 represents a buffer size using the given value

This parameter is not provided, or a given negative value represents the use of the system default buffering mechanism, which uses a row buffer for any class Telegraph (TTY) device, and the other device uses normal buffering. You can use the system default mode in general. Table 9.1 File Object File mode operations

R opens in read mode
Ru or UA opens as read and provides universal line feed support

W Open in writing (empty if necessary)

A opens in Append mode (starting with EOF and creating a new file if necessary)

R+ Open in read-write mode

w+ Open in read/write mode (see W)

A + opens in read/write mode (see a)

RB opens in binary read mode

WB opens in binary write mode (see W)

AB opens in binary append mode (see a)

Rb+ opens in binary read/write mode (see r+)

Wb+ opens in binary read/write mode (see w+)

Ab+ opens in binary read/write mode (see A +)

Some examples of open files:
Fp=open ('/ETC/MOTD ') #以读方式打开
Fp=open (' Test ', ' W ') #以写方式打开
Fp=open (' Data ', ' r+ ') #以读写方式打开
Fp=open (R ' C:\io.sys ', ' RB ') #以二进制读模式打开

1.2.1 Factory function file ()

Any place that uses open () can replace it with file ()

It is recommended to use open () to read and write files, use file () when working with files objects

1.2.2 Universal Line feed support (UNS)

The symbols used by different platforms to represent the end of a line are different, such as \n,\r, or \ r \ n, especially when importing a module. Working with all files in the same way requires UNS.

When you open a file with the ' U ' flag, all line breaks (or line terminators, whatever it is) are replaced with a newline character newline (\ n) when returned via Python's Input method (for example, read* ()). (' RU ' mode also supports ' RB ' option). This feature also supports files that contain different types of line terminators. The Newlines property of the file object records the line terminator of the file it had "seen". If the file has just been opened, the program has not encountered a line terminator. Then the newlines of the file is none. After the first line is read, it is set to the first row of Terminator. If you encounter other types of line terminators, the newlines of the file becomes a tuple that contains each format. Note uns is used only to read text files. No corresponding method for processing file output .

When compiling python, uns is turned on by default. If you don't need this feature, you can use the--without-universal-newlines switch to turn it off when you run the Configure script. If you want to handle the line terminator yourself, consult the core notes , use the relevant properties of the OS module.
1.3 File built-in method open ()

After the successful execution and return of a file object, all subsequent operations on the file will be performed through this "handle". File methods can be divided into four categories: input, output, intra-file movement, and miscellaneous operations

1.3.1 Input

Read () method

Used to read bytes directly into a string and read up to a given number of bytes. If no size parameter is given (the default is-1) or the size value is negative, the file will be read until the end. This method may be removed in a future version.

ReadLine () method

Reads a line of open files (all bytes before the next line terminator). Then the entire line, including the line terminator, is returned as a string. As with read (), it also has an optional size parameter, which defaults to 1, which represents reading to the line terminator. If the parameter is provided, Incomplete rows are returned after the size of a word.

ReadLines () method

does not return a string like the other two input methods. It reads all (the remaining) rows and returns them as a list of strings. Its optional parameter, Sizhint, represents the maximum byte size returned. If it is greater than 0, all rows returned should have approximately Sizhint bytes ( may be slightly larger than this number, because the buffer size needs to be pooled).
1.3.2 Output

The Write () built-in method functions in contrast to read () and ReadLine (). It writes a string containing text data or binary data blocks to a file.

Like ReadLines (), the Writelines () method is a list-based operation that takes a list of strings as arguments and writes them to a file. Line terminator is not automatically added, so if necessary, you must call Writelines () Precede the end of each line with a line terminator. Note There is no "WriteLine ()" method because it is equivalent to calling the Write () method with a single-line string ending with a terminator.

Move within 1.3.3 files

The Seek () method moves the file pointer to a different location in the file. The offset byte represents the relative offset to a position. The default value for the location is 0, which means that the position is calculated from the beginning of the file (that is, the absolute offset), and 1 represents the current position. 2 delegates are counted from the end of the file. When people open files for read and write operations, they are exposed to the Seek () method.

The text () method complements Seek (), which tells you where the current file pointer is located in the file-in bytes from the beginning of the file.
1.3.4 file iterations

File.readlines () to read all the data so that the programmer can release the file resources as soon as possible. If this is not required file.readline () reads one line at a time

File.next () can be used to read the next line of the file

Stopiteration exception is thrown after all row iterations are completed

For Eachline in F.readline ():

:

File iterations are more efficient, and Python code such as writing (and reading) is easier

1.3.5 Other

Close () ends the access to it by closing the file.

Good programming habits require you to close this file before you reassign another file object. If you do not explicitly close the file, you may lose data from the output buffer.

The Fileno () method returns a descriptor for the open file. This is an integer that can be used in some of the underlying operations such as the OS Module (Os.read ()).

Calling the flush () method directly writes the data in the internal buffer to the file immediately, rather than passively waiting for the output buffer to be written. Isatty () is a Boolean built-in function that returns True when a file is a class TTY device, otherwise returns False.truncate () method to intercept the file to the current file pointer position or to a given size, in bytes.
1.3.6 File Methods Miscellaneous

The print statement defaults to the end of the output with a newline character, and a comma after the statement avoids this behavior. The ReadLine () and ReadLines () functions do not handle any whitespace characters in the line (see the practice in this chapter), so you'll need to add commas. If you omit commas, Then the displayed text will have two line breaks after each line, one of which is included with the input, and the other is automatically added by the print statement.

filename = raw_input (' Enter file name: ')
fobj = open (filename, ' W ')
While True:
ALine = Raw_input ("Enter a Line ('. ' To quit):")
If aLine! = ".":
Fobj.write ('%s%s '% (ALINE,OS.LINESEP)
Else
Break
Fobj.close ()

Here we receive one line of input from the user each time, and then save the text to a file. Because Raw_input () does not retain user-entered line breaks, the write () method must be called with a newline character. Also, it is difficult to enter an EOF (End-of-file) character on the keyboard, so the program uses a period (.) As a sign of the end of the file, when the user enters a period, the input is automatically ended and the file is closed. The second example creates a new file in a readable writable mode (possibly emptying an existing file). After writing data to a file, we use the Seek () method to move inside the file using the Tell () method to demonstrate our moving process.
>>>f=open ('/tmp/x ', ' w+ ')
>>>f.tell ()
0
>>>f.write (' Test line 1\n ') #加入一个长为12的字符串 [0-11]
>>>f.tell ()
12
>>>f.write (' Test line 2\n ') #加入一个长为12的字符串 [12-23]
>>>f.tell () #告诉我们当前的位置
24
>>>f.seek ( -12,1) #向后移12个字节
>>>f.tell () #到了第二行的开头
12
>>>f.readline ()
' Test line 2\012 '
>>>f.seek (0,0) #回到最开始
>>>f.readline ()
' Test line 1\012 '
>>>f.tell () #又回到了第二行
12
>>>f.readline ()
' Test line 2\012 '
>>>f.tell () #又到了结尾
24
>>>f.close () #关闭文件

List of built-in methods for file objects

Method actions for File objects

File.close () Close file

File.fileno () returns the descriptor (file descriptor,fd, integer value) of the document

File.flush () flush the internal buffer of the file

File.isatty () Determine if file is a class TTY device

File.next () returns the next line of the file (similar to File.readline ()), or throws a Stopiteration exception when there are no other rows

File.read (Size=-1) reads a size byte from a file, reads all remaining bytes when not given a size or a given negative value, and then returns File.readline (Size=-1) as a string to read from the file and return a row (including the line terminator). or returns the maximum size of characters

File.readlines (sizhint=0) reads all the rows of the file and returns it as a list (containing all the line terminators), and if given Sizhint and greater than 0, the rows with a sum of approximately sizhint bytes are returned ( Size is determined by the next value of the buffer capacity (for example, the buffer size can only be a multiple of 4K, if Sizhint is 15k, then the last return may be 16k Press)

File.xreadlines () is used for iterations that can replace ReadLines () with a more efficient method File.seek (off,whence=0) to move the file pointer in a file, from whence (0 for the file, 1 for the current position, 2 for end of file) offset off byte

File.tell () returns the current position in the file File.truncate (Size=file.tell ()) intercepts the file to the maximum size byte, and defaults to the current file location File.write (str) to write the string to the file

File.writelines (SEQ) writes a string sequence seq to a file

1.4 File built-in properties

Properties of the File object

Property Description of the file object

File.closedtrue indicates that the file has been closed or false

Encoding used by the file.encoding file-when Unicode strings are written to data, they are automatically converted to byte strings using file.encoding and system default encoding if File.encoding is None

The access mode used when the File.modeaccess file is opened

File.name file name

File.newlines is none when the row delimiter is not read, only one row delimiter is a string, and when the file has more than one type of line terminator, it is a list that contains all the currently encountered line Terminators

A file.softspace of 0 means that after outputting a data, a space character is added, and 1 means no. This property is generally not used by programmers, but by internal programs.

1.5 Standard Documents

In general, as soon as your program executes, you can access three standard files. They are standard input (typically keyboard), standard output (buffered output to the monitor), and standard error (non-buffered output to the screen). (The "buffering" and "non-buffering" described here refer to the third argument of the open () function.) These files follow the names in the C language, Stdin,stdout and stderr, respectively. We say "You can access these three standard files as soon as your program executes", meaning that these files have been pre-opened, so you can access them whenever you know their file handles.

A handle to these files can be accessed through the SYS module in Python. After importing the SYS module, you can use Sys.stdin,sys.stdout and sys.stderr to access the. Print statements are typically output to sys.stdout, while the built-in

Raw_input () usually accepts input from the Sys.stdin. Remember that sys.* is a file, so you have to handle the line break yourself. The print statement automatically adds a newline character to the string to be output.

1.6 Command-line arguments

SYS module provides access to command-line arguments through the SYS.ARGV property

SYS.ARGV is a list of command-line arguments, and the first item sys.argv[0] is always the name of the program.

Len (SYS.ARGV) is the number of command-line arguments

1.7 File System

Access to file systems is mostly implemented through Python's OS modules. The module is the primary interface for Python to access the operating system functionality. The OS module is really just the front end of the module that actually loads, and the real "module" obviously relies on the specific operating system.

File/directory Access functions for OS modules

Function description

File processing

Mkfifo ()/mknod () creating Named pipes/Creating File system nodes

Remove ()/unlink () Delete file

Rename ()/renames () renaming files

*stat () returns file information

Symlink () Creating Symbolic links

Utime () Update timestamp

Tmpfile () Create and open (' W+b ') a new temporary file

Walk () generates all filenames under a directory tree

Directory/Folder

ChDir ()/fchdir () change the current working directory/change the current working directory by a file descriptor Chroot () change the root of the current process

Listdir () lists files for the specified directory

GETCWD ()/getcwdu () returns the current working directory/function same, but returns a Unicode object mkdir ()/makedirs () Create directory/create multi-level directory

RmDir ()/removedirs () Delete directory/delete multi-level directory Access/Permissions

Access () Verify permission mode

chmod () Change permission mode

Chown ()/lchown () changes the owner and Groupid/functions the same, but does not track links

Umask () Set default permission mode file descriptor operation

Open () Underlying operating system open, for files, using the standard built-in open () function read ()/write () reads/writes data according to the file descriptor

DUP ()/dup2 () Copy file description symbol/function same, but copy to another file descriptor

Device number

Makedev () Create an original device number from the major and minor device numbers
Major ()/minor () get the Major/minor device number from the original device number

Path name access functions in the Os.path module

Function description

Separated

basename () Remove directory path, return file name

DirName () Remove file name, return directory path

Join () combines parts of the separation into one path name

Split () return (DirName (), basename ()) tuple

Splitdrive () return (drivename,pathname) tuple

Splitext () return (filename,extension) tuple

Information

Getatime () returns the last access time

Getctime () returns file creation time

Getmtime () returns the last file modification time

GetSize () returns the file size (in bytes)

Inquire

Exists () specifies whether the path (file or directory) exists

Isabs () Specifies whether the path is an absolute path

Isdir () Specifies whether the path exists and is a directory

Isfile () Specifies whether the path exists and is a file

Islink () Specifies whether the path exists and is a symbolic link

Ismount () Specifies whether the path exists and is a mount point

Samefile () Two path names pointing to the same file

Python file input and output

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.