1、图片转base64
# !/usr/local/python3.8/bin/python3 # -*- coding:UTF-8 -*- import os import base64 # 图片文件夹是/root/images log_d = '/root/images' logFiles = os.listdir(log_d) # 只转图片格式 suffix = ("png", "jpg", "jpeg") # 在/root/images内遍历文件 for filename in logFiles: print(filename) bool_file = filename.endswith(suffix) if bool_file: with open(filename, 'rb') as f: # image_base64 = base64.b64encode(f.read()) #bytes类型 image = f.read() image_base64 = str(base64.b64encode(image), encoding='utf-8') # 字符串类型 print(image_base64) else: print('文件格式不匹配:{}'.format(filename))
2、解压zip文件
#!/usr/local/python3.8/bin/python3 # -*- coding:UTF-8 -*- import zipfile, os from werkzeug.utils import secure_filename ''' 基本格式:zipfile.ZipFile(filename[,mode[,compression[,allowZip64]]]) mode:可选 r,w,a 代表不同的打开文件的方式;r 只读;w 重写;a 添加 compression:指出这个 zipfile 用什么压缩方法,默认是 ZIP_STORED,另一种选择是 ZIP_DEFLATED; allowZip64:bool型变量,当设置为True时可以创建大于 2G 的 zip 文件,默认值 True; ''' def unzip(path, folder_abs): zip_file = zipfile.ZipFile(path) zip_list = zip_file.namelist() # 得到压缩包里所有文件 for f in zip_list: zip_file.extract(f, folder_abs) # 循环解压文件到指定目录 zip_file.close() # 关闭文件,必须有,释放内存 os.remove(path) def savefile(f, savepath): basepath = os.path.dirname(__file__) upload_path = os.path.join(basepath, savepath, secure_filename(f.filename)) f.save(upload_path) unzip(upload_path, savepath)
3、打包文件zipfile,tarfile
# !/usr/bin/env python # -*- coding:utf-8 -*- # pip install zipfile38,tarfile不需要安装直接导入 import tarfile, zipfile def zipDir(dirpath, outFullName): # dirpath:是要打包的目录, outFullName:是压缩包名字 zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED) for path, dirnames, filenames in os.walk(dirpath): fpath = path.replace(dirpath, '') for filename in filenames: zip.write(os.path.join(path, filename), os.path.join(fpath, filename)) zip.close() def compress_file(dirpath, tarfilename): # tarfilename是压缩包名字,dirname是要打包的目录 if os.path.isfile(dirpath): with tarfile.open(tarfilename, 'w') as tar: tar.add(dirpath) else: with tarfile.open(tarfilename, 'w') as tar: #记录当前工作目录 cur_path = os.getcwd() #切换工作目录 os.chdir(dirpath) for root, dirs, files in os.walk('.'): for single_file in files: # if single_file != tarfilename: filepath = os.path.join(root, single_file) tar.add(filepath) #切换回原来工作目录 os.chdir(cur_path) if __name__ == '__main__': zipDir('D:\\game\\build', 'abc.zip') compress_file('test.txt', 'test.tar.gz') compress_file('/tmp/test', 'test.tar.gz')
4、python逐行读取文件
#!/usr/bin/python3.8 # -*- coding:UTF-8 -*- # 使用fileinput模块 import fileinput for line in fileinput.input("test.txt"): print(line, end='') # 对一个文件对象使用for循环读每行数据 with open('test.txt','r',encoding='utf-8') as f: for line in f: print(line, end='') # 使用readline()函数 with open('test.txt','r',encoding='utf-8') as f: line = f.readline() while line: print(line, end='') # 在 Python 3中使用 line = f.readline() # 一次读取多行数据(f.readlines(2):一次读取两行数据) with open('test.txt','r',encoding='utf-8') as f: tag=True while tag: lines = f.readlines(2) if lines: for line in lines: print(line, end='') else: tag=False
5、读取文件指定行或最后几行
#!/usr/bin/python3.8 # -*- coding:UTF-8 -*- import linecache # 放入缓存防止内存过载 def get_line_count(filename): """ :param filename: 文件名 :return: 返回文件总的行数 """ with open(filename, 'r', encoding="utf-8") as f: count = 1 while True: buffer = f.read(1024 * 1) if not buffer: break count += buffer.count('\n') return count # 读取文件最后几行,方式一:使用seek()方法 def readfileline(lines, filepath, off=-50): """ file.seek(offset[, whence]) file:表示文件对象; whence:作为可选参数,用于指定文件指针要放置的位置,该参数的参数值有 3 个选择:0 代表文件头(默认值)、1 代表当前位置、2 代表文件尾。 offset:表示相对于 whence 位置文件指针的偏移量,正数表示向后偏移,负数表示向前偏移。 例如: 当whence == 0 && offset == 3(即seek(3,0)),表示文件指针移动至距离文件开头处 3 个字符的位置; 当whence == 1 && offset == 5(即seek(5,1)),表示文件指针向后移动,移动至距离当前位置5个字符处; 当whence == 2 && offset == -5(即seek(-5,2)),表示文件指针向前移动,从文件末尾向前移动5个字符。 :param lines: 要读取的行数 :param filepath: 文件路径 :param off: 读取的字符数 :return: """ linenumber = get_line_count(filepath) with open(filepath, 'rb') as f: while True: f.seek(off, 2) res = f.readlines() if len(res) > lines: for i in range(1, len(res)): print(linenumber - lines + i, res[i].decode(), end="") break off += -50 # 读取文件最后几行,方式二:使用linecache模块 def readline(lines, filepath): linecache.clearcache() line_count = get_line_count(filepath) line_count = line_count - (lines - 1) for i in range(lines): last_line = linecache.getline(filepath, line_count) print(line_count, last_line, end="") line_count += 1 def readfile(start, end, filepath): """ :param start: 开始行 :param end: 结束行 :param filepath: 文件路径 :return: """ linecache.clearcache() for i in range(start, end + 1): last_line = linecache.getline(filepath, i) print(i, last_line, end="") if __name__ == '__main__': # 读取第10行到第20行 readfile(10, 20, 'readtest.log') # 读取最后10行 readline(10, 'readtest.log') # 读取最后20行 readfileline(20, 'readtest.log')