基本语法
txt = open(filename)
print txt.read()
filename(文件名)用 argv 或 raw_input 输入。
文件名不区分大小写,但要注意空格。
运行 pydoc open 和 pydoc file 来了解更多,比如:
read(...)
readinto() -> Undocumented. Don't use this; it may go away.
readline(...)
readline([size]) -> next line from the file, as a string.
Retain newline. A non-negative size argument limits the maximum number of bytes to return (an incomplete line may be returned then).
Return an empty string at EOF.
write(...)
write(str) -> None. Write string str to file.
Note that due to buffering, flush() or close() may be needed beforethe file on disk reflects the data written.
writelines(...)
writelines(sequence_of_strings) -> None. Write the strings to the file.
Note that newlines are not added. The sequence can be any iterable object producing strings. This is equivalent to calling write() for each string.
close(...)
close() -> None or (perhaps) an integer. Close the file.
Sets data attribute .closed to True. A closed file cannot be used for further I/O operations. close() may be called more than once without error.
Some kinds of file objects (for example, opened by popen()) may return an exit status upon closing.
Usage:
open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h]
[-b < bundle identifier >]
[-a < application >]
[filenames] [--args arguments]
Help:
Open opens files from a shell.
By default, opens each file using the default application for that file.
If the file is in the form of a URL, the file will be opened as a URL.
Options: (文件的访问模式)
-a Opens with the specified application.
-b Opens with the specified application bundle identifier.
-e Opens with TextEdit.
-t Opens with default text editor.
-f Reads input from standard input and opens with TextEdit.
-F --fresh Launches the app fresh, that is, without restoring windows. Saved persistent state is lost, excluding Untitled documents.
-R, --reveal Selects in the Finder instead of opening.
-W, --wait-apps Blocks until the used applications are closed (even if they were already running).
--args All remaining arguments are passed in argv to the application's main() function instead of opened.
-n, --new Open a new instance of the application even if one is already running.
-j, --hide Launches the app hidden.
-g, --background Does not bring the application to the foreground.
-h, --header Searches header file locations for headers matching the given filenames, and opens them.
close
read
readline()——读取文件的每一行,然后将类似于光标的东西移动到\n 后面,即另一行的起始位置。
truncate
write(stuff)
seek()——运行就回到了文件的起始位置。
三种模式:
(1)f.seek(p,0) 移动文件第 p 个字节处,绝对位置
(2)f.seek(p,1) 移动到相对于当前位置之后的 p 个字节
(3)f.seek(p,2) 移动到相对文章尾之后的 p 个字节
末尾的话,那么偏移量 p 一般是负值。如果是正值,则返回文件末尾位置的偏移量。
def print_two(*args):
arg1, arg2, = args `解包,可跳过,如下 print_two_again`
print "arg1: %r, arg2: %r" % (arg1, arg2)
*args 的星号是告诉 python 把这个函数的所有参数都接收进来,放到一个名为 args(也可以是其他名字,如 a)的列表中。
如果没有星号,会出现这个错误:TypeError:print_two() takes exactly 1 argument (2 given)。因为只定义了一个名为 args 的参数。
这样可以清楚的知道函数有几个变量
def print_two_again(arg1, arg2):
print "arg1: %r, arg2: %r" % (arg1, arg2)
def print_one(arg1):
print "arg1: %r" % arg1
def print_none():
print "I got nothin'."
print_two("zed","shaw")
print_two("zed","shaw","test") # ValueError: too many values to unpack,给的参数个数超过了定义的变量个数。
print_two_again("zed","shaw")
print_one("First!")
print_none()
print_none("?") # TypeError: print_none() takes no arguments (1 given),没有变量可以来接收“?”这个参数。
函数的参数和生成变量时用的=赋值符类似,但传递参数用的是()。
如果一个物件可以用=对其命名,通常也可以将其作为参数传递给一个函数。
我们可以给函数传递的参数的方式有:直接传递给它数字
,也可以是变量
,或数学表达式
,或数学表达式与变量合用
。
定义了一个名为 a 的函数,有 a1,a2 两个变量。
def a(a1, a2):
传递参数的方式:
print "We can just give the function numbers directly:"
a(20,30)
print "OR, we can use variables from our script:"
amount_of_a1 = 10
amount_of_a2 = 50
a(amount_of_a1, amount_of_a2)
print "We can even do math inside too:"
a(10 + 20, 5 + 6)
print "And we can combine the two, variables and math:"
a(amount_of_a1 + 100, amount_of_a2 + 1000)