Dropbox中的文件夹名称

时间:2019-07-11 08:55:25

标签: python dropbox

我在命名Dropbox文件夹和文件时遇到了一些问题

我想将bash脚本转换为python,bash可以正常工作,但是python在其中创建了文件夹日志,其名称类似于“ $(hostname)”,文件名为“ log _ $(date +%F-%H:%M” )_ $(hostname).gz“

import requests
import logging
import os

log = logging.getLogger(__name__)


def test():
    os.system('mkdir - p /var/log/salt/$(hostname)')
    os.system('mv /var/log/salt/minion /var/log/salt/$(hostname)/log_$(date +%F-%H:%M)_$(hostname)')
    os.system('gzip --keep -f /var/log/salt/$(hostname)/log_$(date +%F-%H:%M)_$(hostname)')
    os.system('rm /var/log/salt/minion')
    return {"msg": "log made in Device")


headers = {
    'Authorization': 'Bearer TOKEN',
    'Dropbox-API-Arg': '{"path": "/logs/$(hostname)/log_$(date +%F-%H:%M)_$(hostname).gz"}',
    'Content-Type': 'application/octet-stream',
}

data = open('/var/log/salt/$(hostname)/log_$(date +%F-%H:%M)_$(hostname).gz', 'rb').read()
response = requests.post('https://content.dropboxapi.com/2/files/upload', headers=headers, data=data)
return {"msg": "Copied to Dropbox!"}

1 个答案:

答案 0 :(得分:0)

import requests
 import os
 import datetime
 import socket
 import logging

log = logging.getLogger(__name__)

def test():
    time = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M")
    hostname = socket.gethostname()
    dropboxpath = '/logs/{}/log_{}_{}.gz'.format(hostname, time, hostname)
    filenamelog = '/var/log/salt/{}/log_{}_{}'.format(hostname, time, hostname)
    gzipfilenamelog = filenamelog+'.gz'
    os.system('mkdir - p /var/log/salt/{}'.format(hostname))
    os.system('mv /var/log/salt/minion {}'.format(filenamelog))
    os.system('gzip --keep -f {} > {}'.format(filenamelog, gzipfilenamelog))
    os.system('rm /var/log/salt/minion')
    headers = {
        'Authorization': 'Bearer TOKEN',
        'Dropbox-API-Arg': '{"path":"' + dropboxpath + '"}',
        'Content-Type': 'application/octet-stream'
    }
    data = open(gzipfilenamelog, 'rb').read()
    response = requests.post('https://content.dropboxapi.com/2/files/upload', headers=headers, data=data, timeout=90)
    return {"msg": "Copied to Dropbox!"}