python写入和读取数组文件



import os;

#读取


def myRead():

    for (root, dirs, files) in os.walk("C:/Users/周大哥/Desktop/test"):
        for filename in files:#遍历该文件夹下的每个文件
            filePath=os.path.join(root, filename)#完整的路径
            r=open(filePath)
            last=""
            #一行行读 去掉换行符号
            for temp in r.readlines():
                temp=temp.strip('\n')
                last+=temp


            arr=last.split(' ')#保存到数组里面
            print(filename,"内容为:")
            print(arr)
            r.close()


sum=0

def myWrite(arr):
    global sum
    filePath = "C:/Users/周大哥/Desktop/test"+"/"+str(sum)+".txt"
    sum+=1
    with open(filePath,'w') as f:
        flag=False
        for temp in arr:
            if flag==True:
                f.write(" "+str(temp))
            else:
                flag=True
                f.write(str(temp))

    print("写入完成")
    f.close()

arr=[1,2,3,2,1]
arr1=[23.4444,54]
myWrite(arr)
myWrite(arr1)
myRead()







猜你喜欢

转载自blog.csdn.net/qq_37437892/article/details/80636403