2018年12月15日 其他内置函数与文件操作其他模式

print('0.根据ASICII 反馈字符:',chr(97))
print('1.ord根据字符反馈ASICII:',ord("a"))
print('2.pow,幂函数运算,x**y,x**y%z:',pow(2,3),pow(3,5,4))
print('3.reversed倒序反转:',list(reversed([1,2,3,4])))
print('4.round四舍五入:',round(4.74534))
print('5.set做集合:',set('hello'))
print('6.slice切片,存在start,stop,和step属性:',[1,2,3,4,5][slice(3,5)])#效果同l[3:5]
print('7.sorted 排序',sorted([2,1,5,4,3,6,1]))#同类型比大小,不同类型之间不能比较,高级方法同max和min
print('8.str转化成字符串,与eval相克可以用eval互换:',eval(str({'m':1,'n':2})))
print('9.sum求和:',sum(range(101)))
print('10.type查看数据类型:',type(eval(str({'m':1,'n':2}))))
print('11.vars,参数为list,输出dict',"无参数,同locals(),有参数则用dict方式输出所有方法")
print('12.import 导入模块和:__import__用法:==>\n','import-->system-->__import__()==>\n',"__import__('hello').test()")

print("-"*60)
l=[1,2,6,7,4,2,3,8,9]
print(l[1:7:2]) #切片设置步长
m_d={'m':1,'n':2}
print(eval(str(m_d)))#类型为dict,与原来互换


#分隔符
print("-"*60,"vars")
def test():
    msg="abcdefghi"
    print(locals())
    print(vars())
test()

#分隔符
# print("-"*60,"import")
# import hello #导入hello.py文件,不能导入字符串
# hello.test() #调用hello.py中的test函数
#
#
# print("-"*60,"__import__")
# __import__('hello').test()

#import--->system--->__import__()

 文件处理流程

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

 

  open()

Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError。

注意:使用 open() 方法一定要保证关闭文件对象,即调用 close() 方法。

open() 函数常用形式是接收两个参数:文件名(file)和模式(mode)。

open(file, mode='r')

完整的语法格式为:

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

参数说明:

  • file: 必需,文件路径(相对或者绝对路径)。
  • mode: 可选,文件打开模式
  • buffering: 设置缓冲
  • encoding: 一般使用utf8
  • errors: 报错级别
  • newline: 区分换行符
  • closefd: 传入的file参数类型
  • opener:
f=open('test2',encoding='utf-8') #打开文件,用utf-8打开,open是找的系统的编码,这一行称为句柄,默认打开为只读
print(f.readlines())#读取所有读文字并以列表形式输出
data=f.read()#读取全部文件,加载到内存中,使用read后,把光标移动至最后,如果后面有readline,后面就已经实效了
#print(data)#
f.close()#使用完毕后,需要close 关闭,语柄回收,否则占据操作系统资源
#print(help(open))
#r只读,w只写,a追加

print("-"*50)#分隔符

g=open('test2','r',encoding='utf-8')
print("readable检查打开是否为只读方式",g.readable())
print('readline:每次向下读一行:',g.readline())
print(g.readline(),end="")#注意readline 默认存在换行
print(g.readline())#如果没有那么多行,则为空,不报错

g.close()

 

write

h=open('test3','w',encoding='utf-8')
#w模式,如果test2中有文字,则会清空。如果没有test2文件,则会新建一个文件
#也就是新建一个文件并覆盖
h.write('第一行\n')#只能写str
h.write('第2行\n') #写入test3文件
h.writelines(['abc ','def '])#以列表形式连续写入,

h.close()

append 追加

h=open('test2','a',encoding='utf-8')#a追加模式
h.write("   添加到文件最后")

 

r+模式  又能读又能写,写的时候都是从光标开始write,而且文件都是覆盖,其实不是修改

g=open('test3','r',encoding='utf-8')
data=g.read()#字符串
g.close()

h=open('test2','a',encoding='utf-8')#a追加模式
h.write("   添加到文件最后")
h.write(data)#字符串,将test3中内容全部写入test2中
h.close()

#--------------------

k=open('test2','r',encoding='utf-8')
dk=k.readlines()#以列表形式读取全部
k.close()

print (dk)
l=open ('test3','w',encoding='utf-8')
# dl=l.writelines(dk)#写入全部dk数据
dl=l.write(dk[1])#只写入第2行

l.close()

 

with()

with open('test4','w',encoding='utf-8') as f: #with打开,句柄f,自带close
    f.write('1111\n')

with open('test3','r',encoding='utf-8') as g1,open('test2','w',encoding='utf-8') as g2:   #连续打开
    g1.read()

 

posted @ 2018-12-15 10:18  小圣庄  阅读(152)  评论(0编辑  收藏  举报