python效率提高工具箱

Posted xiewenda8

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python效率提高工具箱相关的知识,希望对你有一定的参考价值。

  1. 读取文件夹调用http请求上传

from urllib3 import encode_multipart_formdata
import requests
import os
import io

PATH='/Users/xiewenda/aaa/'
SUFFIX = '.zip'
files = []
url = ''
print(PATH)

def getFiles(path):
    if path[-1:] != '/':
        path += '/'
    dirFiles = os.listdir(path)
    for file in dirFiles:
        filePath = path + file
        if os.path.isdir(filePath):
            getFiles(filePath + '/')
        else:
            if SUFFIX == '' or file[-4:] == SUFFIX:
                files.append(filePath)

def sendFile(file_path):

	print(file_path)
	arr = file_path.split('/');
	fileName = arr[len(arr)-1];
	with open(file_path, mode='r+') as f:
		fileData = f.read()
		files = 'uploadFiles': (fileName,fileData),'userType':3
		encode_data = encode_multipart_formdata(files)
		file_data = encode_data[0]
		headers_from_data = 
					'Content-Type': encode_data[1] , 
					'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/86.0.4240.183 Safari/537.36',
					'Cookie': 'eloancn.session.id=SESSION_SYSWEB_DC68E953-83F3-41D6-BB1E-C81107B17E0E'
							
		response = requests.post(url, headers=headers_from_data, data=file_data)
		print(response)
		print(response.text)
		
if __name__ == '__main__':
	getFiles(PATH)
	for i in files:
		sendFile(i)
  1. linux定时删除备份文件

linux 创建定时任务
sudo su -
vim /etc/crontab
30 3 * * * root python /backupClear.py >> /backupClear.log
service crond restart

import os
import shutil
from os.path import join, getsize
PATH='/Users/xiewenda/Desktop/aaa/'

def getdirsize(dir):
		size = 0
		for root, dirs, files in os.walk(dir):
			size += sum([getsize(join(root, name)) for name in files])
		return size

if __name__ == '__main__':
	try:
		filesize = getdirsize('/data/log1')
		print filesize
		#文件夹文件大于20GB 时删除文件
		if (filesize / 1024 / 1024 / 1024)>20 :
			print(shutil.rmtree('/data/log'))

	except Exception as e:
		print e
   

以上是关于python效率提高工具箱的主要内容,如果未能解决你的问题,请参考以下文章

python效率提高工具箱

python常用工具让你提高工作效率--常用操作编写,方便自己操作

python提高效率(优化)的心得总结

python提高效率(优化)的心得总结

真香啊,这七大 Python 效率工具真棒啊

磨刀不误砍柴工,PyCharm开发工具的常规配置,充分提高开发效率!