一些Python片段_5


1.获得用户和组信息
#! /usr/bin/env python
#-*- coding: utf-8 -*-
'''获得用户和组信息'''
import os, stat

def WalkDir(dir, file_callback=None):
    for root, dirs, files in os.walk(dir):
        for d in dirs:
            #print d
            if(not d[0] == "."):
                file_path = os.path.join(root, d)
                if file_callback: file_callback(file_path)

    for root, dirs, files in os.walk(dir):
        for f in files:
            #print f
            if(not f[0] == "."):
                file_path = os.path.join(root, f)
                if file_callback: file_callback(file_path)

def LogFile(file):
    try:
        fileStats = os.stat(file)
        #print file
        #print fileStats.st_mode
        #print oct(stat.S_IMODE(fileStats.st_mode))
        #print fileStats.st_uid
        #print fileStats.st_gid
        fileInfo = 'chown ' +  str(fileStats.st_uid) +':' + str(fileStats.st_gid) + ' ' + file
        print fileInfo
    except:
        pass

if __name__ == "__main__":
    path = raw_input('Please input a path to do the test: ')
    WalkDir(path, LogFile)
2.获得文件或者文件夹权限
#! /usr/bin/env python
#-*- coding: utf-8 -*-
'''获得文件或者文件夹权限 '''
import os, stat

def WalkDir(dir, file_callback=None):
    for root, dirs, files in os.walk(dir):
        for d in dirs:
            #print d
            if(not d[0] == "."):
                file_path = os.path.join(root, d)
                if file_callback: file_callback(file_path)

    for root, dirs, files in os.walk(dir):
        for f in files:
            #print f
            if(not f[0] == "."):
                file_path = os.path.join(root, f)
                if file_callback: file_callback(file_path)

def LogFile(file):
    try:
        fileStats = os.stat(file)
        #print file
        #print fileStats.st_mode
        #print oct(stat.S_IMODE(fileStats.st_mode))
        #print fileStats.st_uid
        #print fileStats.st_gid
        fileInfo = 'chmod ' +  oct(stat.S_IMODE(fileStats.st_mode)) + ' ' + file
        print fileInfo
    except:
        pass

if __name__ == "__main__":
    path = raw_input('Please input a path to do the test: ')
    WalkDir(path, LogFile)
3.查找大文件
#!/usr/bin/env python
# coding=utf-8
import os
import os.path
import stat
import sys

def new_queue():
    #the dummy head
    h =  {  "item": None,
            "prev": None,
            "next": None
            }
    h["prev"] = h;
    h["next"] = h;
    return h

def queue_put(h, item):
    p = h["prev"]
    node = {
            "item": item,
            "prev": p,
            "next": h
            }
    p["next"] = node
    h["prev"] = node

def queue_get(h):
    if h["next"] == h:
        return
    p = h["next"]
    h["next"] = p["next"]
    h["next"]["prev"] = h
    return p["item"]

def queue_isempty(h):
    if h["next"] == h:
        return True
    else:
        return False

def travfs_level_order(d):
    if d.startswith("."):
        return
    s = []
    q = new_queue()
    queue_put(q, d)
    while not queue_isempty(q):
        fi = queue_get(q)
        try:
            st = os.stat(fi)
        except:
            continue
        if stat.S_ISDIR(st.st_mode):
            s.append((fi, st.st_size))
            try:
                sub = os.listdir(fi)
            except:
                continue
            for i in sub:
                queue_put(q, os.path.join(fi, i))
        else:
            try:
                size = os.stat(fi).st_size
            except:
                continue
            s.append((fi, size))
    return s

K = 1024
M = K * K
G = M * K

def pretty_size(size):
    if size / G:
        return str(size / G) + "G"
    elif size / M:
        return str(size / M) + "M"
    elif size / K:
        return str(size / K) + "K"
    else:
        return str(size) + "B"

def sort_files(s):
    s.sort(key = lambda x: x[1], reverse=True)

def top_k_size(files, k):
    of = "{:<10}{:<20}"
    for i in range(k):
        try:
            fi = files[i]
        except:
            break
        print of.format(pretty_size(fi[1]), fi[0])

def print_usage():
    print "python travfs.py path nnfind the top n biggest file in a path"

if __name__ == "__main__":
    vlen = len(sys.argv)
    if vlen == 2:
        path = sys.argv[1]
        k = 10
    elif vlen == 3:
        path = sys.argv[1]
        k = int(sys.argv[2])
    else:
        print_usage()
        exit()
    files = travfs_level_order(os.path.abspath(path))
    sort_files(files)
    top_k_size(files, k)

之前的一篇文章中其实有介绍另一个多线程查找Top10大文件的Python代码,非常值得学习。

4.查看端口情况
import os
import struct
import socket

state_table = (
        "EMPTY SLOT",
        "ESTABLISHED",
        "SENT",
        "RECV",
        "WAIT1",
        "WAIT2",
        "WAIT",
        "CLOSE",
        "CLOSE_WAIT",
        "LAST_ACK",
        "LISTEN",
        "CLOSING"
        )

ESTABLISHED = 1
SENT = 2
RECV = 3
WAIT1 = 4
WAIT2 = 5
WAIT = 6
CLOSE = 7
CLOSE_WAIT = 8
LAST_ACK = 9
LISTEN = 10
CLOSING = 11

def decode_addr(name):
    return (socket.inet_ntoa(name[0].decode("hex")[::-1]),
            struct.unpack(">H", name[1].decode("hex"))[0])

def get_tcp():
    f = open("/proc/net/tcp", "r")
    lines = f.readlines()[1:]
    f.close()
    l = []
    for i in lines:
        k = i.strip(" ").split(" ")
        srcall = k[1].split(":")
        src, srcp = decode_addr(srcall)
        dstall = k[2].split(":")
        dst, dstp = decode_addr(dstall)
        tx, rx = k[4].split(":")
        info = {
            "num": k[0],
            "src": src,
            "srcp": srcp,
            "dst": dst,
            "dstp": dstp,
            "state": ord(k[3].decode("hex")),
            "tx": struct.unpack("I", tx.decode("hex"))[0],
            "rx": struct.unpack("I", rx.decode("hex"))[0]
            }
        if k[8]:
            info["uid"] = int(k[8])
        if k[16]:
            info["timeout"] = int(k[16])
        if k[17]:
            info["inode"] = int(k[17])
        l.append(info)
    return l

def pid_fds():
    table = {}
    for i in os.listdir("/proc"):
        try:
            pid = int(i)
        except:
            continue
        basej  = "/proc/%s/fd" % i
        fdtable = {}
        try:
            subs = os.listdir(basej)
        except:
            continue
        for j in subs:
            try:
                k = os.readlink("%s/%s" % (basej, j))
            except:
                continue
            fdtable[int(j)] = k
        table[pid] = fdtable
    return table

def inode_to_pid(inode, pid_fds_table):
    for k,v in pid_fds_table.items():
        for i in v.values():
            if inode == i:
                return k
    return None

def port_to_pid(port):
    tcpinfo = get_tcp()
    pids_table = pid_fds()
    pid = None
    for i in tcpinfo:
        if "inode" not in i:
            continue
        if i["srcp"] != port:
            continue
        pid = inode_to_pid("socket:[%s]" % str(i["inode"]), pids_table)
        if pid:
            break
    return pid

def pid_to_loc(pid):
    f = open("/proc/%d/cmdline" % pid, "r")
    cmdline = f.read().replace("x00", " ")
    f.close()
    return {
            "cwd": os.readlink("/proc/%d/cwd" % pid),
            "exe": os.readlink("/proc/%d/exe" % pid),
            "cmdline": cmdline
    }

def do_tcp():
    of = "{:<5}{:<30}{:<30}{:<20}"
    print of.format("NUM", "SRC", "DST", "STATE")
    for k in get_tcp():
        print of.format(k["num"],
                ":".join((k["src"], str(k["srcp"]))),
                ":".join((k["dst"], str(k["dstp"]))),
                state_table[k["state"]])

def do_port(port):
    pid = port_to_pid(int(port))
    if not pid:
        print "nothing found"
        return
    print "pid: ", pid
    loc = pid_to_loc(pid)
    print "exe: %sncwd: %sncmdline: %s" % (loc["exe"], loc["cwd"], loc["cmdline"])

def print_usage():
    print "-p portt find out which process is binded to this portn-t Display all tcp connections"

if __name__ == "__main__":
    import sys
    if len(sys.argv) < 2:
        print_usage()
        exit(0)
    if sys.argv[1] == "-t":
        do_tcp()
    elif sys.argv[1] == "-p":
        if len(sys.argv) < 3:
            print_usage()
            exit(0)
        do_port(sys.argv[2])
    else:
        print_usage()
5.Python 中如何删除一个列表 List 中多个符合条件的元素?

假设有一个列表:
a = [1, 1, 0, 2, 0, 0, 8, 3, 0, 2, 5, 0, 2, 6]

判断如果a列表中元素’2’前面的一个元素是’0’,那么删除这个元素’0’,请问该如何操作?
========
@刘缙

[x[0] for x in zip(a,a[1:]+[None]) if x!=(0,2)]

又:注意其他答案有些不能处理len(a) in [0,1]的情形
===
@tonic
如果真的是你这样的数据…变字符串join起来把所有的02变成2不就好了?
====
@Simon Luo

for i in range(len(a)-1,-1,-1):
    if a[i]==0 and a[i+1]==2:
        a.pop(i)

参考了python帮助文档里去除list中重复字符的代码。
===
@郭一凡

a = [1, 1, 0, 2, 0, 0, 8, 3, 0, 2, 5, 0, 2, 6]
for i in range(0,len(a)-2):
	if(a[i]==0 and a[i+1]==2):
		del(a[i])
		i=i-1
print a

[1, 1, 2, 0, 0, 8, 3, 2, 5, 2, 6]
===
@绅士提督与不笑船

import json
a = json.loads(json.dumps(a).replace('0, 2', '2'))

稍微实现了一下楼上 @tonic 的说法。
===
@玖道
功能应该能实现,但是如果考虑代码简洁程度或者效率,还是坐等更高明的答案了。

def remove_1(lst):
	ans = []
	for i in range(len(lst)):
		if lst[i] == 0:
			try:
				if lst[i+1] == 2: pass
				else: ans.append(lst[i])
			except: ans.append(lst[i])
		else: ans.append(lst[i])
	return ans

===
@匿名用户

l = [l[i] for i in filter(lambda i: l[i] !=0 or i == len(l) or l[i+1] != 2, range(len(l)))]

@范子逸

[a[i] for i in range(len(a)) if a[i]!= 0 or i+1==len(a) or a[i+1] != 2]

@小生境

a = [1, 1, 0, 2, 0, 0, 8, 3, 0, 2, 5, 0, 2, 6]
a = [x for i, x in enumerate(a[:-1]) if not x and a[i+1] == 2]

用numpy的话比较炫酷:

import numpy
a = numpy.array([1, 1, 0, 2, 0, 0, 8, 3, 0, 2, 5, 0, 2, 6])
b = numpy.lib.stride_tricks.as_strided(a, (len(a)-1, 2), (4, 4))
idx = ~(b == [0, 2]).all(axis=1)
a = a[idx]

最后都要append(a[-1])

a=[a[i] for i in range(len(a)) if not(a[i]==0 and a[i+1]==2)]

原文地址Python 中如何删除一个列表 List 中多个符合条件的元素?

注意可能出现的在进行了一次替代/pop之后可能会再次出现的“0 2”序列,第一种方法我可想不到,唉,o(╯□╰)o

6.自打印的Python脚本
import sys
print __file__
print sys.argv[0]
text = open(__file__).read()
print text[:-1]
#
text2 = open(sys.argv[0]).read()
print text2[:-1]

 

=================

zip、filter、map、reduce、lambda

==Python中zip()函数用法举例 | 南柯一梦
定义:zip([iterable, …])
zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的list(列表)。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。利用*号操作符,可以将list unzip(解压),看下面的例子就明白了:

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped = zip(a,b)
[(1, 4), (2, 5), (3, 6)]
>>> zip(a,c)
[(1, 4), (2, 5), (3, 6)]
>>> zip(*zipped)
[(1, 2, 3), (4, 5, 6)]

==Python 并行遍历zip()函数使用方法
zip()函数在运算时,会以一个或多个序列做为参数,返回一个元组的列表。同时将这些序列中并排的元素配对。

==Python中的map函数、zip函数、filter函数和reduce函数

在本文最上面刘缙的例子中之所以最后可以得到结果,是因为zip()函数本身就是一个迭代对象,会不停的往后进行迭代,所以才有用,唉,无知真可怕!
In [15]: zip?
Type: builtin_function_or_method
String form: <built-in function zip>
Namespace: Python builtin
Docstring:
zip(seq1 [, seq2 […]]) -> [(seq1[0], seq2[0] …), (…)]

Return a list of tuples, where each tuple contains the i-th element from each of the argument sequences. The returned list is truncated in length to the length of the shortest argument sequence.

一些参考链接:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注