Differences between revisions 4 and 5
Revision 4 as of 2004-09-20 08:43:29
Size: 2811
Editor: 61
Comment:
Revision 5 as of 2004-09-20 08:53:55
Size: 2795
Editor: 61
Comment:
Deletions are marked like this. Additions are marked like this.
Line 50: Line 50:
Writing a file a little at a time is more common and less of a problem than reading a file a little at a time.
向文件中每次写入一部分对比于从文件中一次读取一部分更普通,也没有You can just call write and/or writelines repeatedly, as each string or sequence of strings to write becomes ready. Each write operation appends data at the end of the file, after all the previously written data. When you're done, call the close method on the file object. If you have all the data available at once, a single writelines call is faster and simpler. However, if the data becomes available a little at a time, it's at least as easy and fast to call write as it comes as it would be to build up a temporary list of pieces (e.g., with append) to be able to write it all at once in the end with writelines. Reading and writing are quite different from each other, with respect to the performance implications of operating in bulk versus operating a little at a time.
向文件中每次写入一部分对比于从文件中一次读取一部分操作更普通,也没有太多的问题。 可以多次使用'''write'''和(或)'''writelines''', 只要欲写入的字符串或者字符串list准备完毕了。每次写入操作在文件末尾已写入内容之后附加新的数据。 当写文件处理结束后,使用文件对象的'''close'''方法关闭文件。 如果同时有欲写入的全部数据,那么调用简单的''writelines'''方法更快更简单。 否则,每次只能获得欲写入的数据一部分 However, if the data becomes available a little at a time, it's at least as easy and fast to call write as it comes as it would be to build up a temporary list of pieces (e.g., with append) to be able to write it all at once in the end with writelines. Reading and writing are quite different from each other, with respect to the performance implications of operating in bulk versus operating a little at a time.

文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

-- 61.182.251.99 [DateTime(2004-09-20T08:17:12Z)] TableOfContents

描述

写文件

Credit : Luther Blissett

问题

文件中写入文本或2进制数据 ?

解决 Solution

将一个(大)字符串写入文件的最简单的方法如下:

    open('thefile.txt', 'w').write(all_the_text)  # 写入文本到文本文件
    open('abinfile', 'wb').write(all_the_data)    # 写入数据到2进制文件

更好的方法是将文件对象和一个变量绑定,可以及时关闭文件。比如,文本文件写入内容:

    file_object = open('thefile.txt', 'w')
    file_object.write(all_the_text)
    file_object.close(  )

写入文件的内容更多时不是一个大字符串,而是一个字符串的list(或其他序列),这时应该使用writelines方法(此方法同样适用于2进制文件的写操作)

    file_object.writelines(list_of_text_strings)
    open('abinfile', 'wb').writelines(list_of_data_strings)

使用writelines方法, 相对于使用stringjoin方法产生一个大字符串然后写入文件或者循环调用write方法,运行要快许多。

讨论

打开文件以写入,除了文件名外,必须提供第2个参数:w 打开文本文件以供写入, wb 打开2进制文件以写入 在食谱4.2 :读文件中的讨论同样适合于写文件。特别的是,强烈建议文件写操作时显示关闭文件,只有关闭文件才能保证数据确实已经写到了硬盘上而不在内存的临时缓冲区中。

向文件中每次写入一部分对比于从文件中一次读取一部分操作更普通,也没有太多的问题。 可以多次使用write和(或)writelines, 只要欲写入的字符串或者字符串list准备完毕了。每次写入操作在文件末尾已写入内容之后附加新的数据。 当写文件处理结束后,使用文件对象的close方法关闭文件。 如果同时有欲写入的全部数据,那么调用简单的writelines方法更快更简单。 否则,每次只能获得欲写入的数据一部分 However, if the data becomes available a little at a time, it's at least as easy and fast to call write as it comes as it would be to build up a temporary list of pieces (e.g., with append) to be able to write it all at once in the end with writelines. Reading and writing are quite different from each other, with respect to the performance implications of operating in bulk versus operating a little at a time.

   1 

...

参考 See Also

PyCkBk-4-3 (last edited 2009-12-25 07:16:21 by localhost)