>>> print "Hello World!"Hello World!>>> print "The total value is = $", 40.0*45.50The total value is = $ 1820.0>>> print "The total value = $%6.2f" % (40.0*45.50)The total value = $1820.00>>> myfile = file("testit.txt", 'w')>>> print >> myfile, "Hello World!">>> print >> myfile, "The total value = $%6.2f" % (40.0*45.50)>>> myfile.close()
语句时,数据实际未被写入;相反,数据是成批写入的。让 Python 把数据写入文件的最简单方式就是显式地调用
close
方法。
文件对象
file
是与计算机上的文件进行交互的基本机制。可以用
file
对象读取数据、写入数据或把数据添加到文件,以及处理二进制或文本数据。
学习
file
对象的最简单方法就是阅读帮助,如清单 2 所示。
清单 2. 得到 file 对象的帮助
>>> help(file)Help on class file in module __builtin__:class file(object) | file(name[, mode[, buffering]]) -> file object | | Open a file. The mode can be 'r', 'w' or 'a' for reading (default), | writing or appending. The file will be created if it doesn't exist | when opened for writing or appending; it will be truncated when | opened for writing. Add a 'b' to the mode for binary files. | Add a '+' to the mode to allow simultaneous reading and writing. | If the buffering argument is given, 0 means unbuffered, 1 means line | buffered, and larger numbers specify the buffer size. | Add a 'U' to mode to open the file for input with universal newline | support. Any line ending in the input file will be seen as a '\n' | in Python. Also, a file so opened gains the attribute 'newlines'; | the value for this attribute is one of None (no newline read yet), | '\r', '\n', '\r\n' or a tuple containing all the newline types seen. | | 'U' cannot be combined with 'w' or '+' mode. | | Note: open() is an alias for file(). | | Methods defined here:...
>>> myfile = open("testit.txt")>>> myfile.read()'Hello World!\nThe total value = $1820.00\n'>>> str = myfile.read()>>> print str>>> myfile.seek(0)>>> str = myfile.read()>>> print strHello World!The total value = $1820.00>>> str.split()['Hello', 'World!', 'The', 'total', 'value', '=', '$1820.00']>>> str.split('\n')['Hello World!', 'The total value = $1820.00', '']>>> for line in str.split('\n'):... print line... Hello World!The total value = $1820.00>>> myfile.close()
>>> myfile = open("testit.txt")>>> for line in myfile.readlines():... print line... Hello World!The total value = $1820.00>>> myfile.close()>>> for line in open("testit.txt").readlines():... print line... Hello World!The total value = $1820.00>>> for line in open("testit.txt"):... print line... Hello World!The total value = $1820.00
>>> mydata = ['Hello World!', 'The total value = $1820.00']>>> myfile = open('testit.txt', 'w')>>> for line in mydata:... myfile.write(line + '\n')... >>> myfile.close()>>> myfile = open("testit.txt")>>> myfile.read()'Hello World!\nThe total value = $1820.00\n'>>> myfile.close()>>> myfile = open("testit.txt", "r+")>>> for line in mydata:... myfile.write(line + '\n')... >>> myfile.seek(0)>>> myfile.read()'Hello World!\nThe total value = $1820.00\n'>>> myfile.close()>>> myfile = open("testit.txt", "r+a")>>> myfile.read()'Hello World!\nThe total value = $1820.00\n'>>> for line in mydata:... myfile.write(line + '\n')... >>> myfile.seek(0)>>> myfile.read()'Hello World!\nThe total value = $1820.00\nHello World!\nThe total value = $1820.00\n'>>> myfile.close()