python之文件操作

#read 无参数,读全部,有参数,有b按照字节读,无b按字符读


#tell,获取当前指针的位置


#seek,跳转指针的位置


#write,往里写数据,有b写字节,无b写字符


#close,关闭文件

#fileno 文件描述符


#flush,强制把写的内容刷到硬盘里


#read

#readable,是否可读


#writeable,是否可写


#seekable,是否可以移动指针


#readline,只读一行


#truncate,会把指针后面的内容全部干掉,截断文件



#for循环文件对象,循环每一行



#with open('db') as f:  2.7之后,支持同时打开两个文件,with open('db') as f1,with open('db',r) as f2,比如把一个文件的内容
#同时写入到另外一个文件中

# with open('db','r',encoding='utf-8') as f1,open('db_bak','w',encoding='utf-8') as f2:
#     n = 0
#     for line in f1:
#         if n <= 2:
#             f2.write(line)
#             n += 1
#         else:
#             break
#             print('over')


#通过with把f1中的123替换为ABC
# with open('db','r',encoding='utf-8') as f1,open('db_bak','w',encoding='utf-8') as f2:
#     for line in f1:
#         new = line.replace('123','ABC')
#         f2.write(new)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章