使用sys模块

#!/usr/bin/env python
# Filename: using_sys.py
import sys
print('The command line arguments are:')
for s in sys.argv:
    print(s)
print('\n\nThe PYTHONPATH is', sys.path, '\n')

输出:

The command line arguments are:
/home/sy/桌面/python/using_sys.py


The PYTHONPATH is ['/home/sy/桌面/python', '/home/sy', '/usr/bin', '/usr/lib/python3.2', '/usr/lib/python3.2/plat-linux2', '/usr/lib/python3.2/lib-dynload', '/usr/local/lib/python3.2/dist-packages', '/usr/lib/python3/dist-packages'] 

使用命令行参数:

sy@ubuntu:~/桌面$ cd /home/
sy@ubuntu:/home$ cd ./sy/
sy@ubuntu:~$ cd ./桌面/
sy@ubuntu:~/桌面$ cd ./python/
sy@ubuntu:~/桌面/python$ python3 using_sys.py Hello World.
The command line arguments are:
using_sys.py
Hello
World.


The PYTHONPATH is ['/home/sy/桌面/python', '/usr/lib/python3.2', '/usr/lib/python3.2/plat-linux2', '/usr/lib/python3.2/lib-dynload', '/usr/local/lib/python3.2/dist-packages', '/usr/lib/python3/dist-packages'] 

sy@ubuntu:~/桌面/python$ 

换一种方式:

#!/usr/bin/env python
# Filename: using_sys_2.py
from sys import argv
print(argv)
from sys import path
print(path)

打印结果:

['/home/sy/桌面/python/using_sys_2.py']
['/home/sy/桌面/python', '/home/sy', '/usr/bin', '/usr/lib/python3.2', '/usr/lib/python3.2/plat-linux2', '/usr/lib/python3.2/lib-dynload', '/usr/local/lib/python3.2/dist-packages', '/usr/lib/python3/dist-packages']

cat.py

#!/usr/bin/env python
# Filename: cat.py
import sys
def readfile(filename):
    '''Print a file to the standard output.'''
    f = open(filename)
    while True:
        line = f.readline()
        if len(line) == 0:
            break
        sys.stdout.write(line)
    f.close()
# Script starts from here
if len(sys.argv) < 2:
    print('No action specified.')
    sys.exit()
if sys.argv[1].startswith('--'):
    option = sys.argv[1][2:]
    # fetch sys.argv[1] but without the first two characters
    if option == 'version':
        print('Version 1.2')
    elif option == 'help':
        print('''
        This program prints files to the standard output.
        Any number of files can be specified.
        Options include:
        --version: Prints the version number
        --help: Display this help''')
    else:
        print('Unknown option.')
        sys.exit()
else:
    for filename in sys.argv[1:]:
        readfile(filename)

另一个:

#! /usr/bin/env python3.2
# Filename: concatenate.py

import sys
print(sys.version)
print(sys.version_info)
print('concatenate.py')
print()

def readfile(filename):
    '''Print a file to the standard output.'''
    f = open(filename)
    while True:
        line = f.readline()
        if len(line) == 0:
            break
        sys.stdout.write(line)
    f.close()

# Script starts from here.
if len(sys.argv) < 2:
    print('No action specified.')
    sys.exit()
if sys.argv[1].startswith('--'):
    option = sys.argv[1][2:]
    # fetch sys.argv[1] but without the first two characters
    if option == 'version':
        print('Version 1.2')
    elif option == 'help':
        print('''
        This program prints files to the standard output.
        Any number of files can be specified.
        Options include:
        --version: Prints the version number
        --help: Display this help''')
    else:
        print('Unknown option.')
        sys.exit()
else:
    for filename in sys.argv[1:]:
        readfile(filename)



待续





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值