文件(file)操作

文件操作:
1.打开文件,得到文件句柄并赋值给一个变量
2.通过句柄对文件进行操作
3.关闭文件

#能调用方法的一定是对象
# li = [1,2,3]
# li.append('22')#列表对象
# 'asc'.capitalize()#字符串对象
# 昨夜寒蛩不住鸣。
# 惊回千里梦,已三更。
# 起来独自绕阶行。
# 人悄悄,帘外月胧明。
# 白首为功名,旧山松竹老,阻归程。
# 欲将心事付瑶琴。
# 知音少,弦断有谁听。


文件打开方式:
     'r'       open for reading (default)
     'w'       open for writing, truncating the file first
     'x'       create a new file and open it for writing
     'a'       open for writing, appending to the end of the file if it exists
     'b'       binary mode
     't'       text mode (default)
     '+'       open a disk file for updating (reading and writing)
     'U'       universal newline mode (deprecated)


# f = open('小重山','r',encoding='utf8')
# data = f.read()
# #data = f.read(5)  读出前5个字符
# print(data)
#
# f.close()

# #f = open('小重山','w',encoding='utf8')#创建此对象时清空文件内容
# f = open('小重山2','w',encoding='utf8')#没有文件,先创建
# print(f.fileno())句柄
#
# f.write('hello world\n')
# f.write('冷大爷')
# f.close()


import time
f = open('小重山2','a',encoding='utf8')#a --- append
f.write('\n hello world \n')#放入缓冲区
f.write('冷大爷')
#time.sleep(50)
f.close()#写入磁盘,关闭文件

# f = open('小重山2','r',encoding='utf8')

# print(f.readline())
# print(f.readline())
# # readline(self, limit: int = -1) -> AnyStr:
# #         pass

# print(f.readlines())#转换成列表打印
# # readlines(self, hint: int = -1) -> List[AnyStr]:
# #         pass
# number = 0
# for i in f.readlines():
#     number += 1
#     if number == 6:
#     #    # print(i.strip(),'i like it')
#     # else:
#     #     print(i.strip())
#         ##i.strip() +  'i like it'
#         i = ' '.join((i.strip(),"123456"))#字符串拼接用jion
#     print(i.strip())
# f.close()
#
#

# num = 0
# for i in f:    #这是for内部将f对象做成一个迭代器,用一行去一行
#     num += 1
#     if num == 6:
#         i = ''.join([i.strip(),'jkl'])
#
#     print(i.strip())
# f.close()


# print(f.tell())#指出光标位置
# print(f.read(4))
# print(f.tell())
#
# f.seek(0)#调整光标位置
# print(f.read(4))



# import sys,time          #进度条效果
# for i in range(30):
#     sys.stdout.write('*')
#     sys.stdout.flush()
#     time.sleep(0.1)
#
#     # def flush(self, *args, **kwargs):  # real signature unknown
#     #     """
#     #     Finish the compression process.
#     #
#     #     Returns the compressed data left in internal buffers.
#     #
#     #     The compressor object may not be used after this method is called.
#     #     """
#     #     pass


# f = open('小重山2','w',encoding='utf8')
# f.truncate()
# # truncate(self, size: int = None) -> int:
# f.close()

# #r+模式,w+模式,a+模式(可读)
#r+  可读可写  只能在最后写  读时从头读
# f = open('小重山2','r+',encoding='utf8')
# print(f.readline())
# f.write('岳飞')
# f.close()

# #w+  清空后再写  写后光标在最后
# f = open('小重山2','w+',encoding='utf8')
#
# f.write('岳飞')
# print(f.tell())#6----光标在‘岳飞’后面
# f.seek(0)
# print(f.readline())
# f.close()

#a+  在最后添加 从最后读


#怎么修改文件?新建文件,复制
#错误
# f = open('小重山2','r+',encoding='utf8')
# num = 0
# for line in f:
#     num += 1
#     if num == 6:
#         f.write('Jayce')#r w 光标不一致   r从头读  w从最后写
# f.close()

# f_read = open('小重山','r',encoding='utf8')
# f_write = open('小重山2','w',encoding = 'utf8')
# number = 0
# for line in f_read:
#     number += 1
#     if number == 5:
#         line = ''.join([line.strip(),'岳飞\n'])
#         #line = 'hello 岳飞\n'
#     f_write.write(line)
#
# f_read.close()

#作业涉及方法
# a = str({'beijing':{'1':123}})
# print(type(a))
# print(a)    #{'beijing': {'1': 123}}
# a = eval(a)
'''
eval()文档
eval(*args, **kwargs): # real signature unknown
     """
     Evaluate the given source in the context of globals and locals.

    The source may be a string representing a Python expression
     or a code object as returned by compile().
     The globals must be a dictionary and locals can be any mapping,
     defaulting to the current globals and locals.
     If only globals is given, locals defaults to it.
     """
'''
# print(type(a))
# print(a['beijing'])

#with
f = open('log','r')
f.readline()
f.read()
f.close()
#与以下等价   with无须再close
with open('log','r') as f:
     f.readline()
     f.read()
print('冷大爷')

#with同时对两个句柄进行操作
with open('log1','r',encoding='utf8') as f_read , open('log2','w',encoding='utf8') as f_write :
     for line in f_read:
         f_write.write(line)

posted @ 2019-02-27 17:21  冷垚  阅读(282)  评论(0编辑  收藏  举报