參考自 這裡
Preface :
This module implements a helper class and functions to quickly write a loop over standard input or a list of files. If you just want to read or write one file see open().
The typical use is :
If no argument is given, this iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is '-', it is also replaced bysys.stdin. To specify an alternative list of filenames, pass it as the first argument to input(). A single file name is also allowed.
All files are opened in text mode by default, but you can override this by specifying the mode parameter in the call to input() or FileInput(). If an I/O error occurs during opening or reading a file, IOError is raised.
Lines are returned with any newlines intact, which means that the last line in a file may not have one.
You can control how files are opened by providing an opening hook via the openhook parameter to fileinput.input() or FileInput(). The hook must be a function that takes two arguments, filename and mode, and returns an accordingly opened file-like object. Two useful hooks are already provided by this module.
Supported APIs :
The following function is the primary interface of this module :
- fileinput.input([files[, inplace[, backup[, mode[, openhook]]]]])
The following functions use the global state created by fileinput.input(); if there is no active state, RuntimeError is raised.
- fileinput.filename()
- fileinput.fileno()
- fileinput.lineno()
- fileinput.filelineno()
- fileinput.isfirstline()
- fileinput.isstdin()
- fileinput.nextfile()
- fileinput.close()
FileInput Class :
The class which implements the sequence behavior provided by the module is available for subclassing as well :
- class fileinput.FileInput([files[, inplace[, backup[, mode[, openhook]]]]])
Others :
The two following opening hooks are provided by this module :
- fileinput.hook_compressed(filename, mode)
- fileinput.hook_encoded(encoding)
Preface :
This module implements a helper class and functions to quickly write a loop over standard input or a list of files. If you just want to read or write one file see open().
The typical use is :
If no argument is given, this iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is '-', it is also replaced bysys.stdin. To specify an alternative list of filenames, pass it as the first argument to input(). A single file name is also allowed.
All files are opened in text mode by default, but you can override this by specifying the mode parameter in the call to input() or FileInput(). If an I/O error occurs during opening or reading a file, IOError is raised.
Lines are returned with any newlines intact, which means that the last line in a file may not have one.
You can control how files are opened by providing an opening hook via the openhook parameter to fileinput.input() or FileInput(). The hook must be a function that takes two arguments, filename and mode, and returns an accordingly opened file-like object. Two useful hooks are already provided by this module.
Supported APIs :
The following function is the primary interface of this module :
- fileinput.input([files[, inplace[, backup[, mode[, openhook]]]]])
The following functions use the global state created by fileinput.input(); if there is no active state, RuntimeError is raised.
- fileinput.filename()
- fileinput.fileno()
- fileinput.lineno()
- fileinput.filelineno()
- fileinput.isfirstline()
- fileinput.isstdin()
- fileinput.nextfile()
- fileinput.close()
FileInput Class :
The class which implements the sequence behavior provided by the module is available for subclassing as well :
- class fileinput.FileInput([files[, inplace[, backup[, mode[, openhook]]]]])
Others :
The two following opening hooks are provided by this module :
- fileinput.hook_compressed(filename, mode)
- fileinput.hook_encoded(encoding)
沒有留言:
張貼留言