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
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值