实现需要以下步骤:
1、在py文件路径下
2、进入Python 交互模式
3、导入Python文件
4、使用文件名.函数名的方式执行
例如:
[root@xxx /root]
#cat test.py
#!/usr/bin/python
def parse_file():
file_obj = open('/root/test.txt', 'r')
f = file_obj.read()
for date in f.split('\n'):
if 'efficiency:' in date:
efficiency = (date.split(': ')[1])
if 'wastedBytes:' in date:
wastedBytes = (date.split(': ')[1])
if 'userWastedPercent:' in date:
userWastedPercent = (date.split(': ')[1])
print efficiency, wastedBytes, userWastedPercent
#parse_file()
[root@xxx /root]
#pwd
/root
[root@xxx /root]
#python
Python 2.7.5 (default, Nov 11 2020, 14:14:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.parse_file
<function parse_file at 0x7f17ae062578>
>>> test.parse_file()
92.0780 % 80694273 bytes (81 MB) 16.5674 %
>>>
|