函數 | 描述 |
---|---|
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True) | 讀取檔案並回傳檔案串流物件 |
mode 有以下數種
'r' | 讀取(預設) |
'w' | 寫入 |
'a' | 附加 |
'b' | 二進位模式 |
't' | 文字模式(預設) |
'+' | 更新磁碟檔案 |
'U' | 通用新行模式 |
例如我們有以下的純文字檔案,檔名為 data.txt
He who has health has hope. Liberty is the right to do everything which the laws allow. Two heads are better than one.Misery loves company. It is better to win the peace and to lose the war. God's mill grinds slow but sure. It takes all sorts to make a world.There are two sides to every question. Rome was not built in a day. Self-trust is the first secret of success. Every man has his taste. Experience is the extract of suffering. Imagination is more important than knowledge. The End
以下程式讀取 data.txt ,印出檔案物件、檔案物件的型態及 data.txt 的前四行
f = open("data.txt", "r") print(f) print(type(f)) print(f.readline()) print(f.readline()) print(f.readline()) print(f.readline()) # 《程式語言教學誌》的範例程式 # http://pydoing.blogspot.com/ # 檔名:open.py # 功能:示範 Python 程式 # 作者:張凱慶 # 時間:西元 2010 年 12 月
執行結果如下
中英文術語對照 | |
---|---|
函數 | function |
參數 | parameter |
內建函數
- abs(x)
- all(iterable)
- any(iterable)
- ascii(object)
- bin(x)
- bool([x])
- bytearray([source[, encoding[, errors]]])
- bytes([source[, encoding[, errors]]])
- chr(i)
- classmethod(function)
- compile(source, filename, mode, flags=0, dont_inherit=False)
- complex([real[, imag]])
- delattr(object, name)
- dict([arg])
- dir([object])
- divmod(a, b)
- enumerate(iterable, start=0))
- eval(expression, globals=None, locals=None)
- exec(object[, globals[, locals]])
- filter(function, iterable)
- float([x])
- format(value[, format_spec])
- frozenset([iterable])
- getattr(object, name[, default])
- globals()
- hasattr(object, name)
- hash(object)
- help([object])
- hex(x)
- id(object)
- input([prompt])
- int([number | string[, base]])
- isinstance(object, classinfo)
- issubclass(class, classinfo)
- iter(object[, sentinel])
- len(s)
- list([iterable])
- locals()
- map(function, iterable, ...)
- max(iterable[, args...], *[, key])
- memoryview(obj)
- min(iterable[, args...], *[, key])
- next(iterator[, default])
- object()
- oct(x)
- open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
- ord(c)
- pow(x, y[, z])
- print([object, ...], *, sep=' ', end='\n', file=sys.stdout)
- property(fget=None, fset=None, fdel=None, doc=None)
- range([start], stop[, step])
- repr(object)
- reversed(seq)
- round(x[, n])
- set([iterable])
- setattr(object, name, value)
- slice([start], stop[, step])
- sorted(iterable[, key][, reverse])
- staticmethod(function)
- str([object[, encoding[, errors]]])
- sum(iterable[, start])
- super([type[, object-or-type]])
- tuple([iterable])
- type(object)
- vars([object])
- zip(*iterables)
沒有留言:
張貼留言