怎样监测熊掌号绑定成功网站,深圳品牌设计公司深圳品牌设计公司,腾讯企点app,html代码是什么意思一、文件的基本操作 文件内容#xff1a; Somehow, it seems the love I knew was always the most destructive kind
不知为何#xff0c;我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂1、read() 当read()函数中传入整数(int)参数#…一、文件的基本操作 文件内容 Somehow, it seems the love I knew was always the most destructive kind
不知为何我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂1、read() 当read()函数中传入整数(int)参数则读取相应的字符数如果不填写则默认读取所有字符 f open(yesterday2,r,encodingutf-8)
#默认读取全部字符
print(f.read())
f.close()
#输出
Somehow, it seems the love I knew was always the most destructive kind
不知为何我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂f open(yesterday2,r,encodingutf-8)
#只读取10个字符
print(f.read(10))
f.close()
#输出
Somehow, i注只有当文件有读权限时才可以操作这个函数 2、tell() 获取文件句柄所在的指针的位置 f open(yesterday2,r,encodingutf-8)
print(f.read(10))
#获取指针位置
print(f.tell())
f.close()
#输出
Somehow, i #读取的内容
10 #指针位置3、seek() 设置文件句柄所在的指针位置 f open(yesterday2,r,encodingutf-8)
print(f.read(10))
#设置之前的指针位置
print(f.tell())
f.seek(0)
#设置之后的指针位置
print(f.tell())
f.close()
#输出
Somehow, i #读取文件的内容
10 #设置之前的指针位置
0 #设置之后的指针位置4、encoding 打印文件的编码 f open(yesterday2,r,encodingutf-8)
print(f.encoding)
f.close()
#输出
utf-85、fileno() 返回文件句柄在内存中的编号 f open(yesterday2,r,encodingutf-8)
print(f.fileno())
f.close()
#输出
36、name 返回文件名 f open(yesterday2,r,encodingutf-8)
print(f.name)
f.close()
#输出
yesterday27、isatty() 判断是否是一个终端设备(比如打印机之类的) f open(yesterday2,r,encodingutf-8)
print(f.isatty())
f.close()
#输出
False #表示不是一个终端设备8、seekable() 不是所有的文件都可以移动光标比如tty文件可以移动的返回True f open(yesterday2,r,encodingutf-8)
print(f.seekable())
f.close()
#输出
True9、readable() 文件是否可读 f open(yesterday2,r,encodingutf-8)
print(f.readable())
f.close()
#输出
True10、writeable() 文件是否可写 f open(yesterday2,r,encodingutf-8)
print(f.writable())
f.close()
#输出
False #文件不可写11、flush() 写数据时写的数据不想存内存中缓存中而是直接存到磁盘上需要强制刷新 f open(yesterday2,w,encodingutf-8)
#这时hello word在缓存中f.write(hello word)
10
#强刷到磁盘上f.flush()这个怎么实验呢在cmd命令行中cd到你文件所在的路径下然后输入python在Python命令行中输入上面代码 ①cd d:\PycharmProjects\pyhomework\day3下(因为我的被测文件在这个文件夹下) ②在这个目录下输入Python命令行然后进行测试 ③强制刷新之前 ④执行强刷命令之后 ⑤强刷后文件中的内容变化 注以写的模式打开文件写完一行默认它是写到硬盘上去的但是其实它不一定写到硬盘上去了。当你刚写完一行如果此时断电有可能你这行就没有写进去因为这一样还在内存的缓存中内存中的缓存机制所以你有不想存缓存所以就要强制刷新。那一般在什么情况下用呐比如存钱 12、closed 判断文件是否关闭 f open(yesterday2,r,encodingutf-8)
f.read()
print(f.closed)
#输出
False13、truncate(截取字符的数) 截取文件中的字符串打开文件模式一定是追加模式(a)不能是写(w)和读(r)模式 #没有指针
f open(yesterday2,a,encodingutf-8)
f.truncate(10)
f.close()
#截取结果
Somehow, i#有指针
f open(yesterday2,a,encodingutf-8)
f.seek(5)
f.truncate(10)
f.close()
#截取结果
Somehow, i说明truncate截取文件中的字段并不受指针(seek)所在位置影响。 14、write() 写入文件内容 f open(yesterday2,w,encodingutf-8)
f.write(Somehow, it seems the love I knew was always the most destructive kind)
f.close()注:写功能只有当打开文件模式是写(w)或者追加(a)才可操作。 转载于:https://www.cnblogs.com/zhangqigao/articles/6480191.html