文件操作
# file1
# 文件操作:
'''
文件上传:
保存log
系统函数
open(file,mode,buffering,encodeing)
读:
open(path/filename,'rt') ----->返回值:stream(管道)
container = stream.read() ---->读取管道中内容
注意:如果传递的path/filename有误,则会报错:FileNotFoundError
如果是图片则不能使用默认的读取方式,mode = 'rb'
总结:
read()
readline() 每次读取一行内容
readlines() 读取所有的行保存到列表中
readable() 判断是否是可读的
'''
stream = open(r'../../个人工具/base.txt', 'rt', encoding='utf-8') # rt文本文档读
# container = stream.read()
# print(container)
result = stream.readable() # able判断是否可以读取
print(result)
# while True:
# line = stream.readline()
# print(line)
# if not line:
# break
lines = stream.readlines() # 保存到列表中
print(lines)
for i in lines:
print(i)
stream = open(r'../../img/2021-09-26/OHR.MackenzieRiver_ZH-CN0214805768_480x800.jpg', 'rb')
container = stream.read()
print(container)
# file2
# 写文件
'''
stream = open(r'aa.txt', 'w')
mode 是'w'表示写操作
方法:
write(内容) 每次都会将原来的内容清空,然后写当前的内容
writelines(Iterable) 没有换行的效果 4444444GGGGRE
straem.writelines(['赌神:小明明\n', '赌侠笑道\n', '赌圣周星星\n'])
如果有mode='a' 表示追加
'''
straem = open(r'a.txt', 'a', encoding='utf-8')
s = '''
你好!
欢迎来到澳门菠菜,赠送给你一个金币!
赌王:小明
'''
result = straem.write(s)
print(result)
straem.write(' 你需要戒指吗???\n') # 可以\n换行
straem.writelines([' 赌神:小明明\n', ' 赌侠笑道\n', ' 赌圣周星星\n'])
straem.write(' 僵尸先生')
straem.close() # 释放资源
# 关闭之后就无法写入
# file3
# 文件的复制
'''
原文件:../../img/2021-09-26/OHR.MackenzieRiver_ZH-CN0214805768_480x800.jpg
目标文件:./1.jpg
'''
with open(r'./OHR.MackenzieRiver_ZH-CN0214805768_1920x1080.jpg', 'rb') as result:
container = result.read()
with open(r'1.jpg', 'wb') as wstream:
wstream.write(container)
print('文件复制完成')
# os
# 模块:os.py
'''
os.path:
os.path.dirname(__file__) 获取当前文件所在的文件目录(绝对路径)
os.path.join(path,'') 返回的是一个拼接后的新的路径
'''
import os
# print(os.path)
# path = os.path.dirname(__file__) # 显示当前目录的绝对路径
# print(path, '\n', type(path))
# result = os.path.join(path, 'a1.jpg')
# print(result)
with open(r'./OHR.MackenzieRiver_ZH-CN0214805768_1920x1080.jpg', 'rb') as stream: # 要复制的文件
container = stream.read() # 读取文件内容
file = stream.name
filename = file[file.rfind(r'/')+1:]
path = os.path.dirname(__file__)
path1 = os.path.join(path, filename)
with open(path1, 'wb') as wstream:
wstream.write(container)
# file 01
with open('book/a1.txt', 'w') as wstrem:
wstrem.write('hello')
import os
result = os.path.isabs(r'C:\\Users\\Wans\\AppData\\Local\\Programs\\Python39\\lib\\a1.txt')
print('------>', result)
# ------> True
result1 = os.path.isabs(r'../day10_函数/func01.py') # 当前文件的上一级
print('------>', result1)
# ------> False
result1 = os.path.isabs(r'/day10_函数/func01.py') # 找跟file01.py同级别的func01.py
print('------>', result1)
# ------> True
# 获取路径,directory目录 文件夹
# 当前文件所在文件夹的路径
path = os.path.dirname(__file__)
# 通过相对路径得到绝对路径
print(path)
# D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\a.txt
path = os.path.abspath('a.txt')
print(path)
# D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\a.txt
# 获取当前文件的绝对路径
# __file__ 当前文件
path = os.path.abspath(__file__)
print(path)
# D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\file01.py
path = os.getcwd() # 类似os.path.dirname(__file__)
print(path)
# D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件
r = os.path.isfile(os.getcwd()) # isfile判断是不是文件
print(r)
# False
r = os.path.isdir(os.getcwd()) # isdir判断是不是目录
print(r)
# True
path = r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\file01.py'
result = os.path.split(path)
print(result) # 输出前面是路径,后面是文件名
# ('D:\\Users\\Wans\\Desktop\\python stduy\\千峰笔记\\day13_文件', 'file01.py')
print(result[1]) # 这样可以输出文件名
# filename = path[path.rfind('\\')+1:]
# file01.py
result = os.path.splitext(path) # 分割文件与扩展名查看文件的后缀
print(result)
# ('D:\\Users\\Wans\\Desktop\\python stduy\\千峰笔记\\day13_文件\\file01', '.py')
print(os.path.getsize('a.txt')) # 返回文件的大小,单位字节
print(os.path.join(os.getcwd(), 'file', 'a.txt')) # 每多一个加一层
# D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\file\a.txt
'''
os.path: 常用函数
dirname()
join()
split()
splittext()
getsize()
isabs()
isfile()
isdir()
'''
# file 02
# -*- codeing = utf-8 -*-
# @Time : 2021/10/26 19:08
# @Author : Wans
# @File : file02.py
# @Sofware : PyCharm
# os.path里面的函数
# os函数
import os
# os.getcwd()
#
# dir = os.getcwd()
# print(dir)
# # 返回指定目录下的所有的文件和文件夹
# print(os.listdir(r'D:\Users\Wans\Desktop\python stduy\千峰笔记'))
#
# # 创建文件夹
# if not os.path.exists(r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\book'):
# os.mkdir(r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\book')
# os.rmdir(r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\book') # 只能删除空文件夹
#
# os.removedirs(r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\book') # 目录不是空的
# os.remove(r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\book\a1.txt')
# 删除p4文件夹
path = r'/千峰笔记/day13_文件/book'
# filelist = os.listdir(path)
# for file in filelist:
# path1 = os.path.join(path, file)
# os.remove(path1)
# else:
# os.removedirs(path)
# # os.rmdir(path)
# print('删除成功')
path = os.getcwd()
print(path)
# 切换目录
f = os.chdir(r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day12_函数')
print()
path = os.getcwd()
print(path)
'''
os模块下的方法:
os.getcwd() # 获取当前目录
os.listdir() # 浏览文件夹
os.mkdir() # 创建文件夹
os.rmdir() # 删除空的文件夹
os.remove() # 删除文件
os.chdir() # 切换目录
'''
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Wans!
评论
TwikooGitalk