python 3.3.0文件操作的一段代码,有疑问,请教

Python/PHP/Perl 开发与设计
回复
drongh
帖子: 1038
注册时间: 2007-01-10 9:32
送出感谢: 4 次
接收感谢: 5 次

python 3.3.0文件操作的一段代码,有疑问,请教

#1

帖子 drongh » 2013-02-21 11:02

代码: 全选

f = open("somefile.zip", "rb")
g = open("thecopy.zip", "wb")

while True:
    buf = f.read(1024)
    if len(buf) == 0:
         break
    g.write(buf)

f.close()
g.close()
上面的代码是拷贝1024个字节,我测试了一下,somefile.zip这个文件是3M,结果生成了3M的文件,也就是thecopy.zip.
按照程序,应该是1024bytes.

另外len(buf) == 0 这个不时很理解,测试它的长度,怎么就决定拷贝结算了呢?
drongh
帖子: 1038
注册时间: 2007-01-10 9:32
送出感谢: 4 次
接收感谢: 5 次

Re: python 3.3.0文件操作的一段代码,有疑问,请教

#2

帖子 drongh » 2013-02-21 11:03

Files that hold photographs, videos, zip files, executable programs, etc. are called binary files: they’re not organized into lines, and cannot be opened with a normal text editor. Python works just as easily with binary files, but when we read from the file we’re going to get bytes back rather than a string. Here we’ll copy one binary file to another:

1 f = open("somefile.zip", "rb")
2 g = open("thecopy.zip", "wb")

4 while True:
5 buf = f.read(1024)
6 if len(buf) == 0:
7 break
8 g.write(buf)
9
10 f.close()
11 g.close()

There are a few new things here. In lines 1 and 2 we added a "b" to the mode to tell Python that the files are binary rather than text files. In line 5, we see read can take an argument which tells it how many bytes to attempt to read from the file. Here we chose to read and write up to 1024 bytes on each iteration of the loop. When we get back an empty buffer from our attempt to read, we know we can break out of the loop and close both the files.

If we set a breakpoint at line 6, (or print type(buf) there) we’ll see that the type of buf is bytes. We don’t do any detailed work with bytes objects in this textbook.


这个书上的原话。
头像
cuihao
帖子: 4793
注册时间: 2008-07-24 11:33
来自: 郑州
送出感谢: 1 次
接收感谢: 89 次
联系:

Re: python 3.3.0文件操作的一段代码,有疑问,请教

#3

帖子 cuihao » 2013-02-21 11:12

一次读1024字节,写1024字节,不是“拷贝1024个字节”。
就跟 dd if=xxx of=yyy bs=1024 一个道理。

文件读完了就读不出来东西了,所以就算要求读取1024字节,实际得到的还是0,然后可以据此判断文件已经读完。
求人不如求它仨: 天蓝的Wiki 屎黄的Wiki 绿
Site: CUIHAO.TK    Twitter: @cuihaoleo
Machine: Athlon64 X2 5200+ / 2x2GB DDR2-800 / GeForce GTS 450
AD: ~まだ見ぬ誰かの笑顔のために~
drongh
帖子: 1038
注册时间: 2007-01-10 9:32
送出感谢: 4 次
接收感谢: 5 次

Re: python 3.3.0文件操作的一段代码,有疑问,请教

#4

帖子 drongh » 2013-02-21 11:53

谢谢楼上,彻底明白。
回复

回到 “Python/Php/Perl”