在 *nix 系统 (linux/unix) 上,
如果您:
$ chmod 0744 your_file.py
-rwxr--r-- your_file.py
并将 python 的路径添加为第一行your_file.py
:
#!/usr/bin/python
或(在我的情况下):
#!/usr/local/bin/python
一旦你这样做,而不是像这样运行它:
$ python your_file.py
你可以像这样运行它:
$ ./your_file.py
甚至将其重命名为yourfile
并像这样运行它:
$ ./yourfile
如果您随后复制yourfile
到您的 bin(即#!/usr/bin/
,或#!/usr/local/bin/
),您可以像这样运行它:
$ yourfile
那么你就可以...
用于raw_input()
向您的用户征求和获取输入。
your_file.py
:
#!/usr/local/bin/python
import os
while(True):
# cntrl-c to quit
input = raw_input('your_prompt$ ')
input = input.split()
if input[0] == 'ls':
dire = '.'
if len(input) > 1:
dire = input[1]
print('\n'.join(os.listdir(dire)))
else:
print('error')
your_file.py
使用示例:
$ chmod 744 your_file.py
$ cp your_file.py /usr/local/bin/your_file
$ your_file
your_prompt$ ls
list_argv.py
your_file.py
your_ls.py
your_subprocess.py
your_prompt$ ls .
list_argv.py
your_file.py
your_ls.py
your_subprocess.py
your_prompt$ pwd
error
your_prompt$ ^CTraceback (most recent call last):
File "/usr/local/bin/your_file", line 7, in <module>
input = raw_input('your_prompt$ ')
KeyboardInterrupt
$
sys.argv
运行脚本时从命令行获取参数:
list_argv.py
:
#!/usr/local/bin/python
import sys
print(sys.argv)
list_argv.py
使用示例:
$ python list_argv.py
['list_argv.py']
$ python list_argv.py hello
['list_argv.py', 'hello']
$ python list_argv.py hey yo
['list_argv.py', 'hey', 'yo']
$ chmod 744 list_argv.py
$ ./list_argv.py
['./list_argv.py']
$ ./list_argv.py hi
['./list_argv.py', 'hi']
$ ./list_argv.py hey yo
['./list_argv.py', 'hey', 'yo']
$ cp list_argv.py /usr/local/bin/list_argv
$ list_argv hey yo
['/usr/local/bin/list_argv', 'hey', 'yo']
替换raw_input()
为sys.argv
。
'你的_ls.py':
#!/usr/local/bin/python
import sys
import os
dire = '.'
if len(sys.argv) > 1:
dire = sys.argv[1]
print('\n'.join(os.listdir(dire)))
'your_ls.py' 使用示例:
$ chmod 744 your_ls.py
$ cp your_ls.py /usr/local/bin/your_ls
$ your_ls
list_argv.py
your_file.py
your_ls.py
your_subprocess.py
$ your_ls .
list_argv.py
your_file.py
your_ls.py
your_subprocess.py
$ your_ls blah
Traceback (most recent call last):
File "/usr/local/bin/your_ls", line 9, in <module>
print('\n'.join(os.listdir(dire)))
OSError: [Errno 2] No such file or directory: 'blah'
用于subprocess.Popen
从命令行访问您可以访问的任何内容。
your_subprocess.py
:
#!/usr/local/bin/python
import os
import subprocess
while(True):
# cntrl-c to quit
input = raw_input('your_prompt$ ')
process = subprocess.Popen(input, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = process.communicate()
print(out)
print(err)
your_subprocess.py
使用示例:
$ chmod 744 your_subprocess.py
$ cp your_subprocess.py /usr/local/bin/your_subprocess
$ your_subprocess
your_prompt$ ls
list_argv.py
your_file.py
your_ls.py
your_subprocess.py
your_prompt$ ls .
list_argv.py
your_file.py
your_ls.py
your_subprocess.py
your_prompt$ pwd
/Users/ox/_workspace/cmd_ln
your_prompt$ blah
/bin/sh: blah: command not found
your_prompt$ ^CTraceback (most recent call last):
File "/usr/local/bin/your_subprocess", line 8, in <module>
input = raw_input('your_prompt$ ')
KeyboardInterrupt
$
弄坏东西!
:-D
玩得开心!
-牛