学习python,,获取当前目录下的路径


获取当前目录下的路径,目录和文件并写入文本

1 # -*- coding:utf-8 -*-2 import os3 for root,dirs,files in os.walk(os.getcwd()):4  print root,dirs,files 5  open(‘mycd.cdc‘,‘a‘).write("%s %s %s" %(root,dirs,files))

函数化,将同一个目录下的内容写入不同的文件中

# -*- coding:utf-8 -*-import osdef cdWalker(cdrom,cdcfile):   export=""   for root,dirs,files in os.walk(os.getcwd()):      export+="\n %s;%s;%s" % (root,dirs,files)   open(cdcfile,‘w‘).write(export)cdWalker(os.getcwd,‘cd1.cdc‘)cdWalker(os.getcwd,‘cd2.cdc‘)

模拟cmd命令

# -*- coding:utf-8 -*-import sys,cmdclass PyCDC(cmd.Cmd):   def __init__(self):       cmd.Cmd.__init__(self)   def help_EOF(self):       print "退出程序"   def do_EOF(self,line):       sys.exit()   def help_walk(self):       print "扫描光盘内容 walk cd and export into *.cdc"   def do_walk(self,filename):       if filename== "":filename = raw_input("输入cdc文件名::")       print "扫描目录下的内容保存到:‘%s‘" % filename   def help_dir(self):        print "指定保存/搜索目录"   def do_dir(self,pathname):       if pathname=="": pathname = raw_input("输入指定保存/搜索目录:")   def help_find(self):       print "搜索关键词"   def do_find(self,keyword):       if keyword == "":keyword=raw_input("输入搜索关键字:")       print "搜索关键词:‘%s‘" %keywordif __name__==‘__main__‘:   cdc=PyCDC()   cdc.cmdloop()

学习python

评论关闭