Python 交互式运行py文件中的某一个函数

实现需要以下步骤:

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 %
>>> 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值