文件相关操作

    def parse(self, response):
        sourec_file = open('aaa.log', 'r')
        try:
            # 读取所有文件
            # all_the_text = sourec_file.read()
            # print(all_the_text)

            # 文本文件逐行读取
            # for line in sourec_file:
            #     print(line+'====')

            # 读取每行,放入数组
            # list_of_all_the_lines = sourec_file.readlines()
            # print(list_of_all_the_lines)

            # 3.写文件
            # 写文本文件
            # output = open('data', 'w')

            # 写二进制文件
            # output = open('data', 'wb')

            # 追加写文件
            # output = open('data', 'w+')

            # 写数据
            # file_object = open('thefile.txt', 'w')
            # file_object.write(all_the_text)
            # file_object.close( )

            # 写入多行
            # file_object.writelines(list_of_text_strings)
            # 注意,调用writelines写入多行在性能上会比使用write一次性写入要高。
        except Exception as e:
            raise
        else:
            pass
        finally:
            sourec_file.close()

本文固定链接: http://www.js-code.com/python/python_50402.html