文件输入和输出

1. 访问文件并得到流对象

rstream = open('a.txt', encoding='utf-8')  

2.通过流对象进行读操作
r 代表的是读

line = rstream.readline()#读一行
lines = rstream.readlines()#读全部但是会已字符串\n的形式
all = rstream.read()全部 自动换行的
print(all)

3. 释放流对象资源

rstream.close()

with open(r'C:\bank_system\users.txt', 'r') as rstream1:
    all = rstream1.read()
    print(all)

print('--------over------')

写操作
open(‘user.txt’, ‘w’)
只要是写操作,如果没有此文件则默认会创建文件
方法:

       	file = open('itheima.txt', 'w')
		file.write('hello itheima, i am here!')
		file.write('\n')
		file.write('hello itheima, i am here!')
		file.write('\n')
		file.write('hello itheima, i am here!')
		file.close()
       	writelines(list)  ---》 需要在列表中手动添加\n
    
    with open('user.txt', 'a') as wstream:
        wstream.write('明天就是周末啦...')
        wstream.writelines(['hello\n', 'hi\n', '喂\n'])

模块
调用自己包里面的模块,py文件的时候
from 包名.模块明 import *(代表可以访问该模块全部的内容,如果该模块有__all__的话只能访问all里面的)
包里面的包就是:from 包.包.模块

from 模块名 import 具体
from 包名.模块名 import 具体

导入模块中所有内容:
from 模块名 import *
from 包名.模块名 import *

在模块中可以通过 “all=[可以通过访问内容]” 对起到限制作用

init.py
导入包会默认执行的文件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值