python基础学习-文件处理

#文件操作的几种模式
# r,w,a三种模式操作时,需要指定文件的操作模式为utf-8,否则读取的内容会是乱码
#1、r模式:以字符串的方式读取文件中的数据 【默认模式,文件必须存在,不存在则抛出异常】
#2、w模式:以字符串的方式向文件中写入数据,如果文件中本来有数据,写入数据时会清空原来的数据,写入数据时如果没有该文件会新建文件,
#然后再写入数据 【不可读;不存在则创建;存在则清空内容】
#3、a模式:以字符串的方式向文件里面追加数据,追加文件的原数据会保留,如果没有该文件也会新建文件 【不可读;不存在则创建;存在则只追加内容】
#rb,wb,ab三种模式操作时,因为是字节读取,所以不需要指定文件的操作模式为utf-8,但是数据读取出来以后,要将数据编码模式改为utf-8,否则读取出的数据
# 会以字节的方式显示
#4、rb模式:以字节(二进制)的方式读取文件中的数据
#5、wb模式:以字节(二进制)的方式向文件中写数据
#6、ab模式:以字节(二进制)的方式向文件中追加数据
# ****read读取模式:read(read、readline表示每次读取一行数据,默认从第一行开始、
# readlines表示读取文件中的所有行数据
# 文件操作的格式三步骤:
f=open("text.txt","w")#打开文件
f.write("hello world")#读写操作
f.close()#关闭文件
#///文件写入时如果写入中文,必须要指定文件操作模式 ,例如:
file=open("1.txt","w",encoding="utf-8")#指定文件操作模式为utf—8标准模式
print(file.encoding)
file.write("haha,好开心呀!")
file.close()
# ////文件读取'rb'模式,读取后需要对读出的字节进行编码处理
file=open("1.txt","rb")#字节读取不需要指定utf-8标准模式,否则会报错
# result=file.read()#读取文件所有内容
result=file.read(3)#读取指定长度的数据,一个汉字占一个字节
result=result.decode("utf-8")#把读取到的字节转换成utf-8标准模式,否则会显示字节编码不会显示汉字
print(result,type(result))
file.close()

# 文件操作中如果经常忘记关闭文件,这里推荐傻瓜式操作方式:使用with关键字来帮我们管理上下文
# 用法如下:
with open('a.txt', 'w') as f:
pass

with open('a.txt', 'r') as read_f, open('b.txt', 'w') as write_f:
data = read_f.read()
write_f.write(data)


# ===============案例:用户登录==================
inp_username=input('your name>>: ').strip()
inp_password=input('your password>>: ').strip()

# 验证
with open('user.txt',mode='rt',encoding='utf-8') as f:
for line in f:
# print(line,end='') # egon:123\n
username,password=line.strip().split(':')
if inp_username == username and inp_password == password:
print('login successfull')
break
else:
print('账号或密码错误')


# 应用程序====》文件
# 应用程序====》数据库管理软件=====》文件

# 2、w:只写模式,当文件不存在时会创建空文件,当文件存在会清空文件,指针位于开始位置
with open('d.txt',mode='wt',encoding='utf-8') as f:
f.read() # 报错,不可读
f.write('哈哈\n')

# 强调1:
# 在以w模式打开文件没有关闭的情况下,连续写入,新的内容总是跟在旧的之后
with open('d.txt',mode='wt',encoding='utf-8') as f:
f.write('哈哈1\n')
f.write('哈哈2\n')
f.write('哈哈3\n')

# 强调2:
# 如果重新以w模式打开文件,则会清空文件内容
with open('d.txt',mode='wt',encoding='utf-8') as f:
f.write('哈哈1\n')
with open('d.txt',mode='wt',encoding='utf-8') as f:
f.write('哈哈2\n')
with open('d.txt',mode='wt',encoding='utf-8') as f:
f.write('哈哈3\n')

# 案例:w模式用来创建全新的文件
# 文件文件的copy工具

src_file=input('源文件路径>>: ').strip()
dst_file=input('源文件路径>>: ').strip()
with open(r'{}'.format(src_file),mode='rt',encoding='utf-8') as f1,\
open(r'{}'.format(dst_file),mode='wt',encoding='utf-8') as f2:
res=f1.read()
f2.write(res)


# 3、a:只追加写,在文件不存在时会创建空文档,在文件存在时文件指针会直接调到末尾
with open('e.txt',mode='at',encoding='utf-8') as f:
# f.read() # 报错,不能读
f.write('哈哈1\n')
f.write('哈哈2\n')
f.write('哈哈3\n')

# 强调 w 模式与 a 模式的异同:
# 1 相同点:在打开的文件不关闭的情况下,连续的写入,新写的内容总会跟在前写的内容之后
# 2 不同点:以 a 模式重新打开文件,不会清空原文件内容,会将文件指针直接移动到文件末尾,新写的内容永远写在最后


# 案例:a模式用来在原有的文件内存的基础之上写入新的内容,比如记录日志、注册
# 注册功能
name=input('your name>>: ')
pwd=input('your name>>: ')
with open('db.txt',mode='at',encoding='utf-8') as f:
f.write('{}:{}\n'.format(name,pwd))



# # 文件的相关操作**********
# # 对文件或者文件夹的相关操作需要导入os模块
import os #文件和文件夹的操作os模块导入
import shutil #文件操作的高级模块
#创建一个空的文件
file=open("2.txt","w",encoding="utf-8")
file.close()
对文件进行重命名操作=====
os.rename("2.txt","22.txt")#重命名文件名
os.remove("22.txt")#删除文件
os.mkdir("AAA")#创建文件夹,名字叫AAA!!!!!这里是创建一个文件夹
在指定目录里面创建一个文件,4.txt
file=open("AAA/4.txt","w",encoding="utf-8")
file.close()
file=open("AAA/3.txt","w",encoding="utf-8")
file.close()
#获取指定目录下的所有文件名,返回文件名列表======
file_name_list=os.listdir("AAA") #os.listdir()里面不指定参数时,默认查看当前工程里面的所有文件名
print(file_name_list)
result=os.listdir()
print(result)

#查看当前操作目录的路径os.getcwd()
current_path=os.getcwd()
print("当前操作文件夹的路径为:",current_path)
#切换操作的目录os.chdir()
os.chdir("AAA")
current_path=os.getcwd()
print("当前操作文件夹的路径为:",current_path)

# 删除指定目录下的文件
os.remove("AAA/4.txt")
os.remove("AAA/3.txt")
# 重命名文件夹====
os.rename("AAA","BBB")
# 删除空的文件夹====
os.rmdir("BBB")


# 扩展:========
#删除非空目录(os.rmdir只能删除空的文件夹,文件夹不为空时使用会报错)
os.mkdir("CCC")
file=open("CCC/4.txt","w",encoding="utf-8")
file.close()
# 非空文件目录删除需要导入文件操作的高级模块 improt shutil
shutil.rmtree("CCC")#删除目录及目录里面的所有文件

#判断文件是否存在
is_ok=os.path.exists("1.txt")#判断文件是否存在
is_ok=os.path.exists("day01")#判断文件夹是否存在
is_ok=os.path.isfile("1.txt")#判断是否是一个文件
is_ok=os.path.isdir("1.txt")#判断是否是一个目录
print(is_ok)

# 获取文件的文件名和后缀,比如456.txt
result=os.path.splitext("456.txt")
print(result)


#获取路径里面的文件名
my_path="B/C/4.txt"
file_name=os.path.basename(my_path)#取路径里面的文件名
print(file_name)
dir_path=os.path.dirname(my_path)#根据指定路径获取文件夹部分的路径
print(dir_path)


# 根据文件名获取完整路径
result=os.path.abspath("1.txt")
print(result)

#///////必须要掌握的!!!!!
os.listdir()
os.chdir()
os.rename()
os.path.exists()
os.path.basename()


#3. 了解部分 【+不能单独使用,必须配合r、w、a】
# "+" 表示可以同时读写某个文件
# r+, 读写【可读,可写】文件不存在则会报错
# w+,写读【可读,可写】文件不存在会创建新文件
# a+, 写读【可读,可写】文件不存在则会报错
#
# 举例:
with open('g.txt',mode='rt+',encoding='utf-8') as f:
# print(f.read())
f.write('中国')

with open('g.txt',mode='w+t',encoding='utf-8') as f:
f.write('111\n')
f.write('222\n')
f.write('333\n')
print('====>',f.read())


with open('g.txt',mode='a+t',encoding='utf-8') as f:
print(f.read())

f.write('444\n')
f.write('5555\n')
print(f.read())
#
# x, 只写模式【不可读;不存在则创建,存在则报错】
# x+ ,写读【可读,可写】


posted @ 2020-03-13 20:10  sunshine如你  阅读(183)  评论(0编辑  收藏  举报