python 文本的读写

上一篇 / 下一篇  2010-04-13 15:25:02 / 个人分类:Python

myfile = open('file.txt', 'w+')  #open file
myfile.write("this is my test")
myfile.close()

以“写”的方式打开文件,执行结果是this is my test被写入文件。如果不close,在第一次运行的时候,没有写成功,第二次才成功

=========================================================

myfile = open('file.txt', 'r')  #open file
for line in myfile:
    print(line)
myfile.close()

以“读”的方式打开文件,把文件中的内容一行一行打印出来,执行结果不太好,多出空白行:

this is my test

second

third

=========================================================

myfile = open('file.txt', 'r')  #open file
yy = myfile.read()
print (yy)
myfile.close()

以“读”的方式打开文件,把文件内容全部读取出来,执行结果:

this is my test
second
third

==========================================================

myfile = open('file.txt', 'w+')  #open file,
for line in myfile:
    print(line)
myfile.close()

本来是想看看“w”和“w+”有什么不一样的,发现执行结果是file.txt里原本的内容被清空了,屏幕上也没打印什么,原来单执行myfile = open('file.txt', 'w+') 的时候,有清空文件的功效

如果把“w+”改为“w”,就提示出错:

Traceback (most recent call last):
  File "C:\Python30\open file.py", line 11, in <module>
    for line in myfile:
  File "C:\Python30\lib\io.py", line 1739, in __next__
    line = self.readline()
  File "C:\Python30\lib\io.py", line 1813, in readline
    while self._read_chunk():
  File "C:\Python30\lib\io.py", line 1560, in _read_chunk
    input_chunk = self.buffer.read1(self._CHUNK_SIZE)
AttributeError: 'BufferedWriter' object has no attribute 'read1'

===========================================================

myfile = open('file.txt', 'w+')  #open file
myfile.write("this is my test")
for line in myfile:
    print(line)
myfile.close()

执行结果:

文件写入正确,但是屏幕上没有任何输出,怀疑是因为写文件之后,指针是在文件的末尾,于是有以下这段:

myfile = open('file.txt', 'w+')  #open file
myfile.write("this is my test")
myfile.seek(0)
for line in myfile:
    print(line)
myfile.close()

运行结果全都正确,在文本里再加一行呢?

myfile = open('file.txt', 'w+')  #open file
myfile.write("this is my test\nsecond\nthird")
myfile.seek(0)
for line in myfile:
    print(line)
myfile.close()

运行结果:写入正确,输出多了两空白行:(原因是当执行print时,它会另起一行,而被打印的对象里已经包含一次回车)

this is my test

second

third

于是再修改:

myfile = open('file.txt', 'w+')  #open file
myfile.write("this is my test\nsecond\nthird")
myfile.seek(0)
for line in myfile:
    print(line.strip("\r\n"))
myfile.close()

运行结果正确:

this is my test
second
third

在此附上相关信息:

seek(offset[,whence])

Move to new file position. Argumentoffsetis a byte count. Optional argumentwhencedefaults toos.SEEK_SETor0(offset from start of file; offset should be>=0); other values areos.SEEK_CURor1(move relative to current position; offset can be positive or negative), andos.SEEK_ENDor2(move relative to end of file; offset is usually negative, although many platforms allow seeking beyond the end of a file).

offset为偏移量,whence默认为0,表示文件的开头;为1,表示当前问位置;为2表示文件的末尾

offset(1,0):从开头向右跳一位

offset(1,1):从当前位置向右跳一位

offset(-1,1):从当前位置向左跳一位

offset(-1,2):从末尾像左跳一位

 

str.strip([chars])

Return a copy of the string with the leading and trailing characters removed. Thecharsargument is a string specifying the set of characters to be removed. If omitted orNone, thecharsargument defaults to removing whitespace. Thecharsargument is not a prefix or suffix; rather, all combinations of its values are stripped:

>>>'   spacious   '.strip()'spacious'>>>'www.example.com'.strip('cmowz.')'example'

===========================================================

myfile = open('file.txt', 'w+')  #open file
myfile.write("this is my test")
print(myfile)
myfile.close()

执行结果:

文件写入正确,屏幕上输出: <io.TextIOWrapper object at 0x0222E470>

这个太傻了,应该不能直接就输出一个文本吧


TAG: 文本 读写 seek strip

 

评分:0

我来说两句

我的栏目

日历

« 2024-05-05  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 52645
  • 日志数: 17
  • 建立时间: 2009-06-29
  • 更新时间: 2015-04-24

RSS订阅

Open Toolbar