您的位置:首页 > 编程语言 > Python开发

So cute are you python 17

2013-11-16 21:13 239 查看
1.python 实现linux ls 命令:

#!/bin/evn python
#coding:utf-8
#FileName:ls.py
#Function:Show the files or directories on a path.
#history:16-11-2013
import os
path=raw_input("Pleace input the path you want to check._")
print os.listdir(path)


2.python 实现文件的遍历1:

#!/bin/evn python
#FileName:walk.py
#Function:Show the files or directories on a path.
#history:16-11-2013
import os
export =[]
path=raw_input('Pleace input the path you wanna to check._')
for root,dirs,files in os.walk(path):
export.append("\n%s; %s; %s"%(root,dirs,files))
open('myc.txt','w').write(''.join(export))


3.python 实现文件的遍历2:

#!/bin/evn python
#FileName:walk_1.py
#Function:Show the files or directories on a path.
#history:16-11-2013
import os,sys
def cdfiles(cdpath,cdfiles):
export =[]
if(len(cdpath)== 0):
cdpath=raw_input('Pleace input the path you wanna to check._')
for root,dirs,files in os.walk(cdpath):
export.append("\n%s; %s; %s"%(root,dirs,files))
open('myc.txt','w').write(''.join(export))
if __name__=='__main__':
#print sys.argv
if(sys.argv[ 1]=='-e'):
files='myc.txt'
cdfiles(sys.argv[ 1],files)
else:
print '''Usage:_walk_1.py:
python walk_1.py -e /var/ --
store the information into myc.txt.'''


4.python cmd 的使用:

#!/bin/evn python
#coding:utf-8
#FileName:refactoring.py
#Function:Show how to refactoring code.
#history:16-11-2013
import sys,cmd,os
class PyCDC(cmd.Cmd):
def __init__(self,):
cmd.Cmd.__init__(self)
def cd_ls(self,filename,path):
export =[]
if path=="":path=raw_input('请输入搜索的路径_')
for root,dirs,files in os.walk(path):
export.append("\n%s; %s; %s"%(root,dirs,files))
open(filename,'w').write(''.join(export))
def help_EOF(self):
print "退出程序"
def do_EOF(self,line):
sys.exit()
def help_Walk(self):
print '扫描文件内容 写入 *.txt'
def do_walk(self,filename):
spath=""
if filename=="":
filename=raw_input("输入导出文件的*.txt 文件名::")
if spath=="":
spath=raw_input("请输入要搜索的路径::")
export =[]
for root,dirs,files in os.walk(spath):
export.append("\n%s; %s; %s"%(root,dirs,files))
open(filename,'w').write(''.join(export))
print '扫描的内容 存放于 %s'%filename
def help_dir(self):
print '指定保存路径.'
def do_dir(self,path):
if path=="":path=raw_input("Pleace input the path you wanna to search.")
print ""
def help_find(self):
print "搜索关键字."
def do_find(self,keyword):
if keyword=="":keyword=raw_input("输入搜索关键字:_")
print "搜索关键字 %s"%keyword

if __name__=='__main__':
cdc=PyCDC()
cdc.cmdloop()


效果图:

$ python refactoring.py
(Cmd) walk
输入导出文件的*.txt 文件名::myc.txt
请输入要搜索的路径::/var/www/sql
扫描的内容 存放于 myc.txt
(Cmd) help

Documented commands (type help <topic>):
========================================
EOF  dir  find

Miscellaneous help topics:
==========================
Walk

Undocumented commands:
======================
help  walk

(Cmd) EOF
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: