如何解析变量内容,然后根据python评估条件

时间:2016-01-22 05:28:02

标签: python logging

我正在编写一个可以在各种系统上运行的通用python脚本。我遇到了需要检查变量内容的情况。直到现在我的简单代码如下:

log_path = parser.get('default', 'log_path')

# name and location of tar file
tar_file = 'logs.tar.gz'
working_path = os.getcwd()
file_location = os.path.join(working_path,tar_file)

# open conection to s3 and get bucket object
s3_conn = S3Connection()
bucket = s3_conn.get_bucket('smso-eng-log-share')

# make tar.gz
with tarfile.open(tar_file, 'w:gz') as tar:
    tar.add(log_path, arcname='tomcat7')

正在从.ini文件中获取变量log_path

说出log_path = /var/log/tomcat7/的当前值,然后创建名为tomcat7的日志tar。

现在假设log_path = /var/log/haproxy/,它应该使用名称“haproxy

创建

2 个答案:

答案 0 :(得分:1)

使用

os.path.basename(os.path.normpath(log_path))

其中log_path是包含路径名的变量。

示例:

>>> log_path = '/var/log/tomcat7/'
>>> os.path.basename(os.path.normpath(log_path))
>>> 'tomcat7'

答案 1 :(得分:0)

您可以使用"/var/log/haproxy/".split('/')os.path.split("/var/log/haproxy/")来获取路径的一部分。

在doc:https://docs.python.org/2/library/os.path.html

中查看更多功能
相关问题