python的中心哲学

Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The Zen of Python, by Tim Peters Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

IPython和Bash

IPython与Bash输出hello pyyu

[root@py_unix ~]# ipython
Python 2.7. (default, Nov , ::)
Type "copyright", "credits" or "license" for more information. IPython 3.2. -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details. In []: print "hello pyyu"
hello pyyu
[root@py_unix ~]# echo "Hello pyyu"
Hello pyyu

IPython与Bash执行命令

Bash查看当前目录下的文件
[root@py_unix opt]# ls -l ./
总用量
-rw-r--r-- root root 6月 : access.log
-rw-r--r-- root root 6月 : get_ip.py
drwxr-xr-x root root 6月 : mysql
-rw-r--r-- root root 8月 mysql-5.5.-linux2.-x86_64.tar.gz
drwxr-xr-x root root 7月 : redis
In []: import subprocess

In []: subprocess.c
subprocess.call subprocess.check_call subprocess.check_output In []: subprocess.call(["ls","-l","/opt"])
总用量
-rw-r--r-- root root 6月 : access.log
-rw-r--r-- root root 6月 : get_ip.py
drwxr-xr-x root root 6月 : mysql
-rw-r--r-- root root 8月 mysql-5.5.-linux2.-x86_64.tar.gz
drwxr-xr-x root root 7月 : redis
Out[]:

Subprocess与import的过程

In [5]: subprocess.call(["some_command","some_argument","another_argument_or_path"])
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-5-541ed8cab1e8> in <module>()
----> 1 subprocess.call(["some_command","some_argument","another_argument_or_path"]) /usr/lib64/python2.7/subprocess.pyc in call(*popenargs, **kwargs)
522 retcode = call(["ls", "-l"])
523 """
--> 524 return Popen(*popenargs, **kwargs).wait()
525
526 /usr/lib64/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
709 p2cread, p2cwrite,
710 c2pread, c2pwrite,
--> 711 errread, errwrite)
712 except Exception:
713 # Preserve original exception in case os.close raises. /usr/lib64/python2.7/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, to_close, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
1325 raise
1326 child_exception = pickle.loads(data)
-> 1327 raise child_exception
1328
1329 OSError: [Errno 2] No such file or directory

python脚本的运行

[root@py_unix home]# cat ls.py
#!/usr/bin/env python
#python wrapper for ther ls command
import subprocess
subprocess.call(["ls","-l"]) [root@py_unix home]# python ls.py
总用量 4
-rw-r--r-- 1 root root 106 8月 20 09:17 ls.py
[root@py_unix home]# cat osinfo.py
#!/usr/bin/env python
# A System Information Gathering Script
import subprocess
#Command 1
uname = "uname"
uname_arg = "-a"
print "Gathering system information with %s command:\n" % uname
subprocess.call([uname,uname_arg])
#command 2
diskspace = "df"
diskspace_arg = "-h"
print "Gathering diskspace information %s command:\n" %diskspace
subprocess.call([diskspace,diskspace_arg])

输出结果:

[root@py_unix home]# python osinfo.py
Gathering system information with uname command: Linux py_unix 3.10.0-514.21.1.el7.x86_64 #1 SMP Thu May 25 17:04:51 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
Gathering diskspace information df command: 文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/cl-root 17G 3.8G 14G 22% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 184M 831M 19% /boot
tmpfs 98M 0 98M 0% /run/user/0

python和bash查看内核信息和内存:

[root@py_unix home]# cat osinfo.sh
#!/usr/bin/env bash
#A system Information Gathering Script
#Command 1
UNAME="uname -a"
printf "内核信息$UNAME \n\n"
$UNAME #Command 2
DISKSPACE="df -h"
printf "内存信息$DISKSPACE \n\n"
$DISKSPACE
#############
注意Bash的空格

subprocess模块

  加载subprocess模块仅仅是将可以使用的代码文件加载进来。也可以创建自己的模块或文件,拱以后重复使用,这与加载subprocess模块的方法相同。IPython shell的一个非常好的优点就是可以对模块或者文件检查,查看其内部可用的属性。

  使用Tab自动完成查看subprocess中可用的属性:

In []: subprocess.
subprocess.CalledProcessError subprocess.STDOUT subprocess.errno subprocess.mswindows subprocess.signal
subprocess.MAXFD subprocess.call subprocess.fcntl subprocess.os subprocess.sys
subprocess.PIPE subprocess.check_call subprocess.gc subprocess.pickle subprocess.traceback
subprocess.Popen subprocess.check_output subprocess.list2cmdline subprocess.select subprocess.types

查看subprocess.call的信息:

In []: subprocess.call?
Signature: subprocess.call(*popenargs, **kwargs)
Docstring:
Run command with arguments. Wait for command to complete, then
return the returncode attribute. The arguments are the same as for the Popen constructor. Example: retcode = call(["ls", "-l"])
File: /usr/lib64/python2./subprocess.py
Type: function

Python的优点之一是其交互式解释器,也称为shell。shell提供了一种能快速实现灵感、检验特性的方法,以及交互式的模块界面,能够将一些需要两三行脚本才能完成的任务一次性完成。

通常我们编写代码时,会采用同时运行文本编辑器和python的方式(稍后会有介绍,这实际上运行的就是Ipython),通过交互式的使用编辑器和shell,也就是在两者之间切换来完成程序的编写。

IPython集成了交互式Python的诸多优点。IPython具有卓越的Python shell,其性能远远优于标准Python的shell。IPython同时提供了基于控制台命令环境的定制功能,可以十分轻松的将交互式Python shell包含在各种Python应用中,甚至当作shell使用

Ipython提供了两类自动完成:完成(complete)与菜单完成(menu)。两者的差别在于'完成' 尽可能扩展当前的主题词,并提供一个可能的替换列表,而“菜单完成”会扩展主题词,直接匹配可以替换列表中的一个,并且如果连续按Tab键时,每一次都会切换到下一个可能的替换。IPython的默认自动完成是‘完成’。也可以通过设置修改。

强大的魔力函数

IPython有强大的功能。原因之一是它具有非常多的,内建的built-in魔力函数。

输入 按下Tab可以找出所有魔力函数.魔力函数的名字magic本身就具有魔力。运行magic可以打开一个分页的帮助文档,其中记录了所有IPython内建函数的用法。这个帮助文档包括函数名,函数的用法(用于何处),以及函数工作方式的描述。

UNIX Shell

UNIX shell提供了一个处理问题的统一方法,具有丰富的工具集,相当简练容易的语法、标准I/O流、管道、以及重定向等功能。

alias

In []: %alias nss netstat -tunlp
nss |grep 22

牛掰的python与unix的更多相关文章

  1. RNG牛掰!

    2018-05-21 RNG牛掰!Uzi圆梦! 不说了,先去哭了! 2018-07-08 洲际赛后更新,RNG依然牛逼! 2018-08-30 亚运后后更新,UZI加油! 2018-10-22 继续加 ...

  2. Jackson:我是最牛掰的 Java JSON 解析器(有点虚)

    在当今的编程世界里,JSON 已经成为将信息从客户端传输到服务器端的首选协议,可以好不夸张的说,XML 就是那个被拍死在沙滩上的前浪. 很不幸的是,JDK 没有 JSON 库,不知道为什么不搞一下.L ...

  3. python写unix口令破解器

    看了python绝技做出来的unix口令破解器 首先需要crypt. python并不自带!! windows下pip安装失败= = 后来直接去kali敲了 附件:jiami.txt #假设是unix ...

  4. python datetime unix时间戳以及字符串时间戳转换

    将python的datetime转换为unix时间戳 import time import datetime dtime = datetime.datetime.now() ans_time = ti ...

  5. python程序unix密码破解

    # qianxiao996精心制作 #博客地址:https://blog.csdn.net/qq_36374896 #!/usr/bin/env python #指定这是一个python文件,使用这个 ...

  6. html5 canvas(小树姐的牛掰到爆了的作品)

    自从小树嫁了个牛逼的前端之后,canvas的境界超过我了!!! 小树demo 小编表示:这个境界,这个几何,让我有种跪舔的感觉... http://www.wow-trend.com/brand/in ...

  7. 牛人总结python中string模块各属性以及函数的用法,果断转了,好东西

    http://blog.chinaunix.net/uid-25992400-id-3283846.html http://blog.csdn.net/xiaoxiaoniaoer1/article/ ...

  8. X86-64寄存器和栈帧--牛掰降解汇编函数寄存器相关操作

    X86-64寄存器和栈帧 概要 说到x86-64,总不免要说说AMD的牛逼,x86-64是x86系列中集大成者,继承了向后兼容的优良传统,最早由AMD公司提出,代号AMD64:正是由于能向后兼容,AM ...

  9. 小事牛刀之——python做文件对比

    使用python对比filename1和filenam2的差异,并将差异写入到filename3中. #!/usr/bin/env python # -*- coding: utf-8 -*- # @ ...

随机推荐

  1. rancher下的kubernetes之二:安装rancher和kubernetes

    在上一章<rancher下的kubernetes之一:构建标准化vmware镜像>,我们做了个通用的虚拟机镜像,可以root登录,apt已经更新,docker也装好了,现在我们就来安装ra ...

  2. python(五):面向对象--类和实例

    一.类的基本概念 类是用来创建数据结构和新类型对象的主要机制.一个类定义了一系列与其实例对象密切关联的属性.典型的属性包括变量(也被称为 类变量)和函数(又被称为方法). 1.class上下文 cla ...

  3. C#多线程应用:子线程更新主窗体控件的值(二)

    在上篇文章中,我已经给大家列了一个在主线程中实现的方式,这篇文章来给大家说说使用Invoke的方式的例子: 对于不代理不太熟悉的朋友,建议先查查相关资料: 例子一: 在C#中,直接在子线程中对窗体上的 ...

  4. 02 - Unit07:显示笔记下拉菜单、笔记的分享功能、笔记的删除功能

    显示笔记下拉菜单 笔记的分享功能 发送Ajax请求 绑定事件:绑定分享按钮单击事件 参数获取:笔记ID 发送请求:/share/add.do 服务器处理 ShareController ShareSe ...

  5. VS2017开发Linux平台上的程序

    重装系统后安装VS2015时卡住了,于是试试看VS2017怎样,听说还支持调Linux.发现VS2017跟12/13/15又有了新的飞跃,竟然支持模块化下载,对于我这种主要写C++简直是个福音,勾了L ...

  6. 彻底解密C++宽字符(一)

    彻底解密C++宽字符(一) 转:http://club.topsage.com/thread-2227977-1-1.html 1.从char到wchar_t “这个问题比你想象中复杂” 从字符到整数 ...

  7. 黄聪:wordpress如何防止发布文章时候自动清除<P>、<br>换行标签

    1.安装[TinyMCE Advanced]插件 2.进入[后台]--[设置]--[TinyMCE Advanced]把这个选项勾上保存即可.

  8. Java 经典练习题_Day010

    final 变量能被显式地初始化并且只能初始化一次.被声明为 final 的对象的引用不能指向不同的对象.但是 final 对象里的数据可以被改变.也就是说 final 对象的引用不能改变,但是里面的 ...

  9. 十、jdk工具之Jdb命令(The Java Debugger)

    目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...

  10. Waiting for device dev/disk/by-id/ata-...-part2 to appear

    问题: 平台:Oralce VM Virtualbox的虚拟机Opensuse11.4 导出该机器的OVA文件后,把该OVA文件导入虚拟机,开机启动时报如下错误: Trying manual resu ...