2012年5月10日 星期四

[Python Std Library] Built-in Types : File Objects


譯自 這裡
File Objects :
File objects are implemented using C’s stdio package and can be created with the built-in open() function. File objects are also returned by some other built-in functions and methods, such as os.popen() and os.fdopen() and the makefile() method of socket objects. Temporary files can be created using the tempfile module, and high-level file operations such as copying, moving, and deleting files and directories can be achieved with the shutil module.

When a file operation fails for an I/O-related reason, the exception IOError is raised. This includes situations where the operation is not defined for some reason, like seek() on a tty device or writing a file opened for reading.

下面為 File object 提供的函數 :
- file.close()
關閉 file 物件. 被關閉後的 File 物件無法再進行讀寫動作, 如果嘗試對一個關閉的 File 物件讀寫會產生 ValueError 例外. 另外從 Python 2.5 你可以使用 with statement 在File 物件操作完畢後自動關閉該 File 物件. 底下為範例代碼 :
  1. from __future__ import with_statement # This isn't required in Python 2.6  
  2.   
  3. with open("hello.txt") as f:  
  4.     for line in f:  
  5.         print line  
在比較舊版本的代碼, 可能如下開啟/關閉 File 物件 :
  1. f = open("test.txt")  
  2.   
  3. try:  
  4.     for line in f:  
  5.         print("Line:{}".format(line.strip()))  
  6. finally:  
  7.     f.close()  

- file.flush()
Flush the internal buffer, like stdio‘s fflush(). This may be a no-op on some file-like objects.
Note flush() does not necessarily write the file’s data to disk. Use flush() followed by os.fsync() to ensure this behavior.

- file.next()
參考 Python Iterator Type, 因為這個方法, 你可以如下使用 for loop 一行行取出 File 物件的內容 :

- file.read([size])
從檔案讀取最多 size bytes 的內容. 如果 size 為負值或者沒有給, 則讀取全部內容.
>>> fh = open('test.txt')
>>> fh.read() # 讀取檔案所有內容.
'Hi John.\nThis is a test doc\nLine1\nLine2\nLine3\nEnd of line'
>>> fh.read(8) # 因為讀寫頭已經到 EOF, 故讀出為空字串.
''
>>> fh.seek(0) # 將讀寫頭返回 BOF (Begin of File)
0
>>> fh.read(8) # 讀取 8 bytes 內容.
'Hi John.'

- file.readline([size])
如果 size 沒給的話, 返回一行的內容 (含 '\n') ; 如果 size 為大於零的值, 則返回最多 size 的內容 (遇到換行, 則會停下來而讀取少於 size 的內容).

- file.readlines([sizehint])
如果 sizehint 沒給的話, 將檔案內容以行為單位, 返回串列 ; 如果 sizehint 有給的話, 返回最少包含 sizehint 大小的行數(不含 '\n'), 並以串列回傳.
>>> fh = open('test.txt')
>>> fh.readlines()
['Hi John.\n', 'This is a test doc\n', 'Line1\n', 'Line2\n', 'Line3\n', 'End of line']
>>> fh.seek(0)
>>> fh.readlines(8) # 讀取行數至少包含 8 bytes. 'Hi John.' 共 8 bytes, 故返回第一行.
['Hi John.\n']
>>> fh.seek(0)
>>> fh.readlines(9) # 因為第一行不夠 9 bytes, 故返回第一, 二行.
['Hi John.\n', 'This is a test doc\n']

- file.seek(offset[, whence])
將操作位置移到 offset 的地方 ; whence 為 offset 相對的點, 值可以是 os.SEEK_SET or 0 (預設), os.SEEK_CUR or 1 與 os.SEEK_END or 2.
>>> fh = open('test.txt')
>>> fh.read() # 讀取所有內容
'Hi John.\nThis is a test doc\nLine1\nLine2\nLine3\nEnd of line'
>>> fh.seek(3) # 將讀寫頭移到第 3 byte, 下一個從第 4 byte 讀起.
>>> fh.read(4)
'John'

- file.tell()
返回目前已操作的最後一個位置.
>>> fh = open('test.txt')
>>> fh.read(7) # 讀取 7 bytes
'Hi John'
>>> fh.tell()
7

- file.write(str)
寫入 str 的內容到檔案, 寫入不代表I/O 完成, 有可能會先在 buffer 中, 等到一定大寫才進行 IO 操作. 可以使用 flush() or close() 強迫 IO 操作.
>>> fh = open('test2.txt', 'w') # 使用 'w' 模式開啟檔案 'test2.txt'
>>> fh.write('Hi John\n')
8 # 共寫入 8 bytes
>>> fh.write('Second line')
11
>>> fh.close()
>>> fh = open('test2.txt')
>>> fh.read()
'Hi John\nSecond line'

- file.writelines(sequence)
sequence 為 Sequence Types, 這個方法將 sequence 的內容依序寫入檔案中.
>>> fh = open('test2.txt', 'w')
>>> fh.writelines(['line1\n', 'line2', 'line3\n', 'line4'])
>>> fh.close()
>>> fh = open('test2.txt')
>>> fh.read()
'line1\nline2line3\nline4'

以上為檔案物件提供的方法, 其實檔案物件上也有某些屬性值得你參考 :
- file.closed
此為唯獨屬性, 判斷檔案是否已經關閉. 當你呼叫 close() 會改變此屬性.
>>> fh = open('test2.txt')
>>> fh.closed
False
>>> fh.close()
>>> fh.closed
True

- file.mode
唯讀屬性, 為你使用 open() 打開檔案時的 I/O mode (如果沒有指定, 預設是 'r').
>>> fh = open('test2.txt')
>>> fh.mode
'r'
>>> fh.close()
>>> fh.mode
'r'
>>> fh = open('test2.txt', 'w')
>>> fh.mode
'w'

- file.name
唯讀屬性, 代表你使用 open() 打開的檔案名稱.
>>> fh = open('test2.txt', 'w')
>>> fh.name
'test2.txt'
This message was edited 25 times. Last update was at 20/03/2012 15:26:41

[Python Std Library] File and Directory Access : tempfile — Generate temporary files and directories


參考自 這裡
Preface :
This module generates temporary files and directories. It works on all supported platforms.

In version 2.3 of Python, this module was overhauled for enhanced security. It now provides three new functions, NamedTemporaryFile(), mkstemp(), and mkdtemp(), which should eliminate all remaining need to use the insecure mktemp() function. Temporary file names created by this module no longer contain the process ID; instead a string of six random characters is used.

Also, all the user-callable functions now take additional arguments which allow direct control over the location and name of temporary files. It is no longer necessary to use the global tempdir and template variables.

Supported APIs :
The module defines the following user-callable functions :
- tempfile.TemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None]]]]])
Return a file-like object that can be used as a temporary storage area. The file is created using mkstemp(). It will be destroyed as soon as it is closed (including an implicit close when the object is garbage collected). Under Unix, the directory entry for the file is removed immediately after the file is created. Other platforms do not support this; your code should not rely on a temporary file created using this function having or not having a visible name in the file system.

The mode parameter defaults to 'w+b' so that the file created can be read and written without being closed. Binary mode is used so that it behaves consistently on all platforms without regard for the data that is stored. bufsize defaults to -1, meaning that the operating system default is used.

The dir, prefix and suffix parameters are passed to mkstemp().

- tempfile.NamedTemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None[, delete=True]]]]]])
New in version 2.3.
New in version 2.6: The delete parameter.

This function operates exactly as TemporaryFile() does, except that the file is guaranteed to have a visible name in the file system (on Unix, the directory entry is not unlinked). That name can be retrieved from the name attribute of the file object. Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later). If delete is true (the default), the file is deleted as soon as it is closed.

- tempfile.SpooledTemporaryFile([max_size=0[, mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None]]]]]])
New in version 2.6.
This function operates exactly as TemporaryFile() does, except that data is spooled in memory until the file size exceeds max_size, or until the file’s fileno() method is called, at which point the contents are written to disk and operation proceeds as with TemporaryFile().

The resulting file has one additional method, rollover(), which causes the file to roll over to an on-disk file regardless of its size.

- tempfile.mkstemp([suffix=''[, prefix='tmp'[, dir=None[, text=False]]]])
New in version 2.3.
Creates a temporary file in the most secure manner possible. There are no race conditions in the file’s creation, assuming that the platform properly implements theos.O_EXCL flag for os.open(). The file is readable and writable only by the creating user ID. If the platform uses permission bits to indicate whether a file is executable, the file is executable by no one. The file descriptor is not inherited by child processes.

Unlike TemporaryFile(), the user of this api is responsible for deleting the temporary file when done with it.

If suffix is specified, the file name will end with that suffix, otherwise there will be no suffix. This api does not put a dot between the file name and the suffix; if you need one, put it at the beginning of suffix.

If dir is specified, the file will be created in that directory; otherwise, a default directory is used. The default directory is chosen from a platform-dependent list, but the user of the application can control the directory location by setting the TMPDIR, TEMP or TMP environment variables.

If prefix is specified, the file name will begin with that prefix; otherwise, a default prefix is used.

If text is specified, it indicates whether to open the file in binary mode (the default) or text mode. On some platforms, this makes no difference.

This api returns a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file, in that order.
>>> tf = tempfile.mkstemp(suffix='.tmp', prefix='john')
>>> tf
(3, 'c:\\users\\john\\appdata\\local\\temp\\johncpv5ll.tmp') # 產生暫存檔 c:\\users\\john\\appdata\\local\\temp\\johncpv5ll.tmp
>>> os.write(tf[0], 'test string'.encode('utf-8'))
11 # 總共寫入 11 bytes
>>> os.close(tf[0])

- tempfile.mkdtemp([suffix=''[, prefix='tmp'[, dir=None]]])
New in version 2.3.
Creates a temporary directory in the most secure manner possible. There are no race conditions in the directory’s creation. The directory is readable, writable, and searchable only by the creating user ID.

The user of this API is responsible for deleting the temporary directory and its contents when done with it.

The prefix, suffix, and dir arguments are the same as for mkstemp().

This API returns the absolute pathname of the new directory.

Global Variables :
The module uses two global variables that tell it how to construct a temporary name. They are initialized at the first call to any of the functions above. The caller may change them, but this is discouraged; use the appropriate function arguments, instead.
- tempfile.tempdir
When set to a value other than None, this variable defines the default value for the dir argument to all the functions defined in this module.

If tempdir is unset or None at any call to any of the above functions, Python searches a standard list of directories and sets tempdir to the first one which the calling user can create files in. The list is :

- tempfile.gettempdir()
New in version 2.3.
Return the directory currently selected to create temporary files in. If tempdir is not None, this simply returns its contents; otherwise, the search described above is performed, and the result returned.

- tempfile.gettempprefix()
New in version 1.5.2.
Return the filename prefix used to create temporary files. This does not contain the directory component.

Supplement :
* [Python Std Library] Built-in Types : File Objects
File objects are implemented using C’s stdio package and can be created with the built-in open() function...

* stackoverflow > TypeError: 'str' does not support the buffer interface
If you use Python3x then string is not the same type as for Python 2.x, you must cast it to bytes (encode it)....

This message was edited 28 times. Last update was at 11/05/2012 09:49:01

[Python Std Library] File and Directory Access : fileinput — Iterate over lines from input stream


參考自 這裡
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]]]]])
Changed in version 2.5: Added the mode and openhook parameters.
Create an instance of the FileInput class. The instance will be used as global state for the functions of this module, and is also returned to use during iteration. The parameters to this function will be passed along to the constructor of the FileInput class.

The following functions use the global state created by fileinput.input(); if there is no active state, RuntimeError is raised.
- fileinput.filename()
Return the name of the file currently being read. Before the first line has been read, returns None.

- fileinput.fileno()
New in version 2.5.
Return the integer “file descriptor” for the current file. When no file is opened (before the first line and between files), returns -1.

- fileinput.lineno()
Return the cumulative line number of the line that has just been read. Before the first line has been read, returns 0. After the last line of the last file has been read, returns the line number of that line.

- fileinput.filelineno()
Return the line number in the current file. Before the first line has been read, returns 0. After the last line of the last file has been read, returns the line number of that line within the file.

- fileinput.isfirstline()
Returns true if the line just read is the first line of its file, otherwise returns false.

- fileinput.isstdin()
Returns true if the last line was read from sys.stdin, otherwise returns false.

- fileinput.nextfile()
Close the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count towards the cumulative line count. The filename is not changed until after the first line of the next file has been read. Before the first line has been read, this function has no effect; it cannot be used to skip the first file. After the last line of the last file has been read, this function has no effect.

- fileinput.close()
Close the sequence.


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]]]]])
Changed in version 2.5: Added the mode and openhook parameters.
Class FileInput is the implementation; its methods filename(), fileno(), lineno(), filelineno(), isfirstline(), isstdin(), nextfile() and close() correspond to the functions of the same name in the module. In addition it has a readline() method which returns the next input line, and a __getitem__() method which implements the sequence behavior. The sequence must be accessed in strictly sequential order; random access and readline() cannot be mixed.

With mode you can specify which file mode will be passed to open(). It must be one of 'r', 'rU', 'U' and 'rb'.

The openhook, when given, must be a function that takes two arguments, filename and mode, and returns an accordingly opened file-like object. You cannot useinplace and openhook together.

Optional in-place filtering: if the keyword argument inplace=1 is passed to fileinput.input() or to the FileInput constructor, the file is moved to a backup file and standard output is directed to the input file (if a file of the same name as the backup file already exists, it will be replaced silently). This makes it possible to write a filter that rewrites its input file in place. If the backup parameter is given (typically as backup='.'), it specifies the extension for the backup file, and the backup file remains around; by default, the extension is '.bak' and it is deleted when the output file is closed. In-place filtering is disabled when standard input is read.

Others :
The two following opening hooks are provided by this module :
- fileinput.hook_compressed(filename, mode)
New in version 2.5.
Transparently opens files compressed with gzip and bzip2 (recognized by the extensions '.gz' and '.bz2') using the gzip and bz2 modules. If the filename extension is not '.gz' or '.bz2', the file is opened normally (ie, using open() without any decompression).

Usage example: fi = fileinput.FileInput(openhook=fileinput.hook_compressed)

- fileinput.hook_encoded(encoding)
New in version 2.5.
Returns a hook which opens each file with codecs.open(), using the given encoding to read the file.

Usage example: fi = fileinput.FileInput(openhook=fileinput.hook_encoded("iso-8859-1"))

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...