Python(十)文件存储

文件操作

    文件操作,涉及到对文件读、写、执行等

    打开一个文件open()

    

    打开一个文件:

 

    

file = open("/Users/haowenchao/Downloads/python/file/hello.txt","r")

 

file1Str1=list()

file1Str2=list()

file1Str3=list()

print(type(file1Str1))

count = 1

print("#####################################################")

for eachline in file:

        if(count==1):

                if(eachline[:6]=="======"):

                        print("count=" + str(count))

                        count +=1

                        continue

                else:

                        file1Str1.append(list(eachline))

        if(count==2):

                if(eachline[:6]=="======"):

                        print("count=" + str(count))

                        count +=1

                        continue

                else:

                        file1Str2.append(list(eachline))

        if(count==3):

                print("count=" + str(count))

                file1Str3.append(list(eachline))

file.close()

print(file1Str1)

print("-----------------------------------------------------")

print(file1Str2)

print("-----------------------------------------------------")

print(file1Str3)

print("-----------------------------------------------------")

print("#####################################################")

 

 

------------------------------------------------------------------------------------------------------------------------------------------

 

改进版:

 

file = open("/Users/haowenchao/Downloads/python/file/hello.txt","r")

file1 = open("/Users/haowenchao/Downloads/python/file/file1.txt","w")

file2 = open("/Users/haowenchao/Downloads/python/file/file2.txt","w")

file3 = open("/Users/haowenchao/Downloads/python/file/file3.txt","w")

file1Str1=list()

file1Str2=list()

file1Str3=list()

print(type(file1Str1))

count = 1

print("#####################################################")

for eachline in file:

        if(count==1):

                if(eachline[:6]=="======"):

                        print("count=" + str(count))

                        count +=1

                        continue

                else:

                        file1Str1.append(list(eachline))

        if(count==2):

                if(eachline[:6]=="======"):

                        print("count=" + str(count))

                        count +=1

                        continue

                else:

                        file1Str2.append(list(eachline))

        if(count==3):

                print("count=" + str(count))

                file1Str3.append(list(eachline))

file.close()

print(file1Str1)

file1.write(str(file1Str1));

print("-----------------------------------------------------")

print(file1Str2)

file2.write(str(file1Str2));

print("-----------------------------------------------------")

print(file1Str3)

file3.write(str(file1Str3));

print("-----------------------------------------------------")

print("#####################################################")

file1.close()

file2.close()

file3.close()

 

猜你喜欢

转载自awenhaowenchao.iteye.com/blog/2409033
今日推荐