1 Star 0 Fork 0

Ragus/pycs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
devop_backup_host.py 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
import os
from datetime import datetime
import requests
import json
class Log:
"""logging system"""
def __init__(self,homepath, logfilename):
self.homepath = homepath
self.logfilename = logfilename
def inet_addr(self):
"""return public ip address"""
print "return public ip address..."
return requests.get("http://httpbin.org/ip").json()['origin']
def files_attr(self, path):
"""return home path files attributes"""
print "return file list...", path
fls = []
for p,ds,fs in os.walk(path):
for f in fs:
print str(os.path.join(p,f))
# 使用','.join("%s" % x for x in LIST),是为了避免抛出 sequence item 0 expected string int found的错误
fls.append( str(os.path.join(p,f)) + ':' + ','.join("%s"%x for x in os.stat(os.path.join(p,f))))
return json.dumps(fls)
def record(self):
"""write into log file"""
print "write into file..."
with open(self.logfilename,'a') as f:
sep = '-' * 40
f.write(sep)
f.write(str(datetime.now()))
f.write(sep + '\n')
f.write(self.inet_addr() + '\n')
f.write(self.files_attr(self.homepath) + '\n')
if __name__ == '__main__':
HOME_PATH = os.path.join('your','real','path')
LOG_FILENAME = os.path.join(os.getcwd(),'log.txt')
print LOG_FILENAME
log = Log(HOME_PATH,LOG_FILENAME)
log.record()
print "finished..."
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/ragus/pycs.git
git@gitee.com:ragus/pycs.git
ragus
pycs
pycs
master

搜索帮助