import os print(os.getcwd()) f=open(r'D:\练习存放\date.txt') os.mkdir(r'D:\练习存放2') os.chdir(r'D:\练习存放2') os.getcwd() first=open('first.txt','w',encoding='utf8') first.write('超级赛亚人蓝色') names=['tom','tim'] names_1=[name+'\n' for name in names] first.writelines(names) first.writelines(names_1) first.flush() #原本不关闭无法看到写入的元素,但使用flush可以先将缓存写入硬盘 first.close() #open('路径','模式',encoding='编码')打开文件,创建文件,r读取,w写入,rw读取并写入,a追加(append),*b二进制(这个是字节的形式操作) f.read() #读取文件.read(N)一次性读取多少字节或者自负 print(f.read()+'a') f.seek(0) #.seek(n)------移动鼠标位置 print(f.read()) f.close() #.close()关闭文件连接 a=open(r'D:\练习存放\date.txt') print(a.read(4)) print(a.read(4)) print(a.read(4)) '''输出 a 啦啦啦德玛西亚 德玛西亚 德玛 啦啦啦德 玛西亚 德玛西亚 ''' a.seek(0) l=a.readlines() #.readlines()读取行到列表 m=a.readline() #.readline()读取下一行 print(m) print(l) with open('first.txt','rw',encoding='utf8') as first: first.write('hello\n') print(first.read())
关于python文件的读取创建写入
最新推荐文章于 2024-07-06 01:02:47 发布