1.使用示例 如何读取压缩文件的示例: importgzipwithgzip.open('file.txt.gz','rb')asf:file_content=f.read() 如何创建压缩的GZIP文件的示例
downloadthem.ifnotos.path.exists(IRIS_TRAINING):raw=urlopen(IRIS_TRAINING_URL).read()withopen(IRIS_TRAINING,"wb")asf
5.例子 读取CSV文件的最简单示例: importcsvwithopen('some.csv','rb')asf:reader=csv.reader(f)forrowinreader:printrow
这个例子展示了一个简单的使用方法mmap: importmmap#writeasimpleexamplefilewithopen("hello.txt","wb")asf:f.write("HelloPython
withopen(FLAGS.graph,"rb")asf: 这里我们得到了一个文件句柄,用于我们传递给脚本的路径 ifFLAGS.input_binary:graph_def.ParseFromString
下面的代码将在退出块时自动关闭fwith: from__future__importwith_statement#Thisisn'trequiredinPython2.6withopen("hello.txt")asf
Convertfileexisting_db.dbtoSQLdumpfiledump.sqlimportsqlite3,oscon=sqlite3.connect('existing_db.db')withopen('dump.sql','w')asf