文章目錄
1
2
3
4
5
6
7
#!/usr/bin/python
# -*- coding=UTF8 -*-

import os
import commands
import re
from optparse import OptionParser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def mytest():
a=input("input a number: ")
s=[]
if a < 10:
for i in range(a):
s.append(i)
elif a < 100:
for i in range(a)[0:a:10]:
s.append(i)
else:
while a > 0:
a=a/100
s.append(a)
return s

print([e for e in mytest() if e>1]) #重点

res=map((lambda x: x+3),mytest())
del res[0]
print('re=%s retype=%s' % (str(res),str(type(res))))

print(dir("")) #打印string的全部属性和方法

m = re.search('[0-9]','abcd4ef') #参考http://www.cnblogs.com/vamei/archive/2012/08/31/2661870.html
print(m.group(0))

class myclass(object):
cname="myclass"
def __init__(self):
print("myclass init")
return
def show(self,str):
self.str=str
print(str)
return

myc=myclass()
myc.show("show")
print(myc.str)
print(myc.cname)

ret=os.system("ls")
print(ret)
print os.popen('ls').readlines()
files=commands.getoutput("ls")
files=files.split('\n')
print(files)

def main():
try:
usage = "usage: %prog [options] arg"
parser = OptionParser(usage)
parser.add_option("-f", "--file", dest="filename",
help="read data from FILENAME")
(options, args) = parser.parse_args()
if options.filename:
print "reading %s..." % options.filename
fpath=options.filename
if os.path.exists(fpath)==True:
f=open(fpath,"r+")
print(f.readlines())
f.write("#writefile")
f.close()
except KeyboardInterrupt:
print "用户中断"
except Exception, e:
print e

return

if __name__ == "__main__":
main()

推荐python教程 http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html

文章目錄