标签:bcf import dll wow color ice Fix 二进制 bmc
with open("d:\\python\\a.txt","r",encoding = "utf-8") as fp: print(fp.read())
class Sample: def __enter__(self): print("In __enter__()") return "Foo" def __exit__(self, type, value, trace): print("In __exit__()") def get_sample(): return Sample() with get_sample() as sample: print("sample:", sample)
#!/usr/bin/env python # -*- coding: utf-8 -*- fp = open("d:\\python36\\a.txt","r",encoding = "utf-8") data = fp.read() fp.close() print(data)
#!/usr/bin/env python # -*- coding: utf-8 -*- fp = open("d:\\python36\\a.txt","r",encoding = "utf-8") data = fp.readline() fp.close() print(data)
>>> fp = open("d:\\python36\\a.txt","r") >>> while 1: ... content = fp.readline() ... print(content) ... if content == "": ... break ... hello world! hello world! hello world! hello world! hello world! hello world! hello world! hello world! >>> fp.close()
#!/usr/bin/env python # -*- coding: utf-8 -*- fp = open("d:\\python36\\a.txt","r",encoding = "utf-8") #data = fp.readline() data = fp.readlines() fp.close() print(data)
>>> fp = open("d:\\python36\\a.txt","r+") >>> fp.read() ‘test1\ntest2\ntest3\nalex4\nalex5\n\n‘ >>> fp.tell() 37 >>> fp.write("hello") 5 >>> fp.seek(0.0) 0.0 >>> fp.read() ‘test1\ntest2\ntest3\nalex4\nalex5\n\nhello‘ >>> fp = open("d:\\python36\\a.txt","a+") >>> fp.read() ‘‘ >>> fp.tell() 42 >>> fp.seek(0,0) 0 >>> fp.read() ‘test1\ntest2\ntest3\nalex4\nalex5\n\nhello‘ >>> fp.tell() 42 >>> fp.seek(0,0) 0 >>> fp.write("world!") 6 >>> fp.seek(0,0) 0 >>> fp.read() ‘test1\ntest2\ntest3\nalex4\nalex5\n\nhelloworld!‘ >>> fp.close() >>> fp = open("d:\\python36\\a.txt","w+") >>> fp.tell() 0 >>> fp.read() ‘‘ >>> fp.write("hello world!") 12 >>> fp.read() ‘‘ >>> fp.seek(0,0) 0 >>> fp.read() ‘hello world!‘ >>> fp.close()
>>> with open("d:\\python\\a.txt","r") as f: ... f.read(1) ... ‘g‘ >>> fp = open("d:\\python\\a.txt","r") >>> fp.read(1) ‘g‘ >>> >>> fp.read(1) ‘l‘ >>> fp.read(1) ‘o‘ >>> fp.read(1) ‘r‘ >>> fp.read(1) ‘y‘ >>> fp.read(2) ‘ r‘ >>> fp.read(2) ‘oa‘ >>> fp.read(2) ‘d ‘ >>> fp.read(2) ‘is‘ >>> fp.seek(0,0) 0 >>> fp.readline(3) ‘glo‘ >>> fp.readline(3) ‘ry ‘ >>> fp.readline(3) ‘roa‘ >>> fp.readline(5) ‘d is ‘ >>> fp.readline(5) ‘great‘
#!/usr/bin/env python # -*- coding: utf-8 -*- fp = open("a.txt", "r", encoding="utf-8") data = fp.readlines() fp.close() lines = 0 for i in data: if "test" in i: lines += 1 #print(i) print(lines)
#!/usr/bin/env python # -*- coding: utf-8 -*- fp = open("d:\\python36\\a.txt","w",encoding = "utf-8") fp.write("test1\n") fp.write("test2\n") fp.close fp = open("d:\\python36\\a.txt","r",encoding = "utf-8") print(fp.read())
#!/usr/bin/env python # -*- coding: utf-8 -*- for i in range(1,11): fp = open("d:\\python36\\" + str(i)+".txt","w",encoding = "utf-8") fp.close()
#!/usr/bin/env python # -*- coding: utf-8 -*- with open("d:\\python36\\a.txt","r",encoding = "utf-8") as fp: while(fp.readline()): print(fp.readline())
#!/usr/bin/env python # -*- coding: utf-8 -*- fp = open("d:\\python36\\a.txt","w") print ("文件是否关闭:", fp.closed) print ("文件的访问模式:", fp.mode) print ("文件名称:", fp.name) #关闭文件 fp.close()
>>> fp = open("d:\\python\\a.txt","w") >>> fp.writelines(["1\n","2\n","3\n"]) >>> fp.close()
testList = [‘test1\n‘, ‘test2\n‘, ‘test3‘, ‘文件操作‘] fp = open( "c:\\downloads\\ip2.txt",‘w+‘) print (fp.read(),end="\n") fp.writelines(testList) fp.flush() fp1 = open( "c:\\downloads\\ip2.txt",‘r‘) print (fp1.read(),end="\n") fp.close() fp1.close()
>>> fp = open("d:\\python\\a.txt","r") >>> print(fp.fileno()) 3 >>> fp.close()
#!/usr/bin/env python # -*- coding: utf-8 -*- with open("d:\\python36\\a.txt","r",encoding = "utf-8") as fp: print(fp.tell()) print(fp.readline()) print(fp.tell())
#!/usr/bin/env python # -*- coding: utf-8 -*- with open("d:\\python36\\a.txt","r",encoding = "utf-8") as fp: print(fp.tell()) print(fp.readline()) fp.seek(0,0) print(fp.tell())
#!/usr/bin/env python # -*- coding: utf-8 -*- with open("d:\\python36\\a.txt","r",encoding = "utf-8") as fp: fp.readline() fp.seek(3,0) # 从第三个位置开始读取 print(fp.tell()) print(fp.readline())
#!/usr/bin/env python # -*- coding: utf-8 -*- with open("d:\\python\\a.txt","r+",encoding = "utf-8") as fp: fp.truncate(10)
#!/usr/bin/env python # -*- coding: utf-8 -*- import linecache file_content= linecache.getlines(‘d:\\python\\a.txt‘) print (file_content) file_content =linecache.getlines(‘d:\\python\\a.txt‘)[0:4] print (file_content) file_content =linecache.getline(‘d:\\python\\a.txt‘,2) print (file_content) file_content =linecache.updatecache(‘d:\\python\\a.txt‘) print (file_content) #更新缓存 linecache.checkcache(‘d:\\python\\a.txt‘) #清理缓存,如果你不再需要先前从getline()中得到的行 linecache.clearcache()
#!/usr/bin/env python # -*- coding: utf-8 -*- fp = open("d:\\python\\a1.txt","r+") aList = [] for item in fp: if item.strip(): aList.append(item) fp.close() fp = open("d:\\python\\a2.txt",‘w‘) fp.writelines(aList) fp.close()
#!/usr/bin/env python # -*- coding: utf-8 -*- import pickle as p shoplistfile = ‘d:\\shoplist.data‘ # the name of the file where we will store the object shoplist = [‘apple‘, ‘mango‘, ‘carrot‘] # Write to the file f = open(shoplistfile, ‘wb‘) p.dump(shoplist, f) # dump the object to a file f.close() del shoplist # remove the shoplist # Read back from the storage f = open(shoplistfile,‘rb‘) storedlist = p.load(f) print (‘从文件读取的列表对象:‘,storedlist)
#!/usr/bin/env python # -*- coding: utf-8 -*- import pickle as p shoplistfile = ‘e:\\shoplist.data‘ # the name of the file where we will store the object shoplist = [‘apple‘, ‘mango‘, ‘carrot‘] animallist=[‘hippo‘,‘rabbit‘] # Write to the file f = open(shoplistfile, ‘wb‘) p.dump(shoplist, f) # dump the object to a file p.dump(animallist,f) f.close() del shoplist # remove the shoplist del animallist # Read back from the storage f = open(shoplistfile,‘rb‘) storedlist = p.load(f) animallist= p.load(f) print (storedlist) print (animallist)
标签:bcf import dll wow color ice Fix 二进制 bmc
原文地址:https://www.cnblogs.com/maohao369/p/9835489.html