使用命令行执行python代码

1. 简单例子

# file_name: cmd_func_test.py
import math
import sys

sum = int(sys.argv[1]) + int(sys.argv[2])
print(sum)

cmd命令:
python .\cmd_func_test.py 1 2

2. 使用命令行调用函数

# file_name: test_cmd_multi_func.py
def my_sum(a, b):
    print(a + b)

def my_sub(a, b):
    print(a - b)



def process_files(path):
    return path

def process_files_2(path):
    print(path)


cmd命令:
python -c "import test_cmd_multi_func as funcs; funcs.my_sum(1,2)"
python -c "import test_cmd_multi_func as funcs; funcs.process_files_2('hello')"

3. 基于__main__ 调用函数:


def process_files_2(path):
    print(path)

if __name__ == "__main__":
    import sys
    import os
    process_files_2(sys.argv[1])

cmd命令:
python .\test_cmd_multi_func.py 'hello'

你可能感兴趣的:(Python数据分析)