zoukankan      html  css  js  c++  java
  • Applications of Python

    import xml.dom.minidom
    import os
    import sys
    import zipfile
    import shutil
    import datetime
    from os import path
    
    def main():
        #working with xml
        doc=xml.dom.minidom.parse("samplexml.xml");
        print doc.nodeName
    
        print doc.childNodes
        print doc.firstChild.tagName
    
        skills = doc.getElementsByTagName("skill")
        print("%d skill " %  skills.length)
        counter = 0
        for skill in skills:
            counter +=1
            print "Skill # ",counter,"-",skill.getAttribute("name")
    
        newSkill=doc.createElement("skill")
        newSkill.setAttribute("name","JavaScript")
        doc.firstChild.appendChild(newSkill)
    
        skills = doc.getElementsByTagName("skill")
        print ("%d Skill  " % skills.length)
        for skill in skills:
            print skill.getAttribute("name")
    
        xmlfile = "samplexml.xml"
        fh = open(xmlfile)
        counter = 0
        for line in fh:
            counter +=1
            if line.startswith("<?xml") & counter ==1:
                print "yes ,this is an xml file"
            else:
                print "not ,it is not an xml file"
    
    #working with file
        f= open("file.txt",'r')   # reading only
        outfile = open("newfile.txt",'w')
        #f.xreadlines()
        for line in f :
            outfile.write(line)
    
        # writing Large files
        bigfile = open("bigfile.txt",'w')
        for i in range(100000):
            bigfile.write("%d 
    " % i )
    
        buffersize = 50000
        infile=open( "bigfile.txt",'r')
        outfile =open( "newbigfile.txt",'w')
        buffer=infile.read(buffersize)    # read the file much faster than usual  50 k
    
        while len(buffer):
            outfile.write(buffer)    #   50 kB each time
            print "good"
            buffer=infile.read(buffersize)
        print "done"
    
        # reading and writing Binary Data  just like a pic
        inputfileb=open("28.jpg",'rb')
        outputfile=open("29.jpg",'wb')
        buffersize=5000
        buffer = inputfileb.read(buffersize)
        while len(buffer):
            outputfile.write(buffer)
            buffer=inputfileb.read(buffersize)
        print "Pic copy done "
    
    
        # File System
    
        if path.exists("file.txt"):
            scr = path.realpath("file.txt")
    
        else:
            src = "file is not eixt with this name"
        print scr
    
        head,tail = path.split(scr)
        print "head ",head
        print "tail ", tail
    
        print "Making copy of file!"
        dst = scr + ".bak"
        shutil.copy(scr,dst)
    
        shutil.copystat(scr,dst)
        #root_dir ,tail = path.split(scr)
        #shutil.make_archive("archive",'zip',root_dir)
        print 145
        # parsing Directory tress
    
        print 123
        paths ="C:Logs"
        for files in os.walk(paths):
    
            print
            for file in files :
                print file
        print "all done"
    
    
        # process zip files  ,put files into zip
        fh = zipfile.ZipFile("myzipdata.zip",'w')
        fh.write("bigfile.txt")
        fh.write("newfile.txt")
        fh.close()   # this is need  ,if this not close  the next statement can not reconigse this zip file
        #reading from zip files
    
        fr = zipfile.ZipFile('myzipdata.zip','r')   # the issue is identify above
        names = fr.namelist()
        for name in names:
            print ("processing %s" % name)
            fhr = open(name)
            contents =  fhr.read()
    
    # working with modules
    
    # using Standard Library Modules
        now = datetime.datetime.now()
        print now
        print now.year , now.month,now.day,now.hour
    
    #working withe the sys module
    # using sys module with command line argumens
    
    
    main()
    print "this script name is " , sys.argv[0]
    print path.split(sys.argv[0])   # output ('C:/Users/IBM_ADMIN/PycharmProjects/OnePython/com/app', 'Applications.py')
    if len(sys.argv) > 1 :
        print "there are " ,len(sys.argv) -1 ,'arguments'
        for arg in sys.argv[1:]:
            print arg
    else:print "there are no argument"
    # checking host platform
    print sys.platform   # win32
    if sys.platform=="win32":
        import ntpath
        pathmodule = ntpath
    print pathmodule   # out put <module 'ntpath' from 'C:Python27lib
    tpath.pyc'>
    #redirect output
    old  = sys.stdout
    sys.stdout=open("output.txt",'w')
    print "this file should be print into file output.txt"
    sys.stdout=old
    print "come back "

  • 相关阅读:
    javascript无提示关闭窗口,兼容IE,Firefox
    vbs简单制作U灵大盗带发送功能的代码
    正则表达式提取网址、标题、图片等一例(.Net Asp Javascript/Js)的实现
    在asp.net中保持Session的有效期
    批处理编程 介绍
    IIS日志清理CMD版,VBS版,JS版,WSH版
    正则表达式提取图片地址
    用U盘安装GNU/Linux
    三核浏览器Lunascape新版发布
    黑客基础之DOS(最齐全)
  • 原文地址:https://www.cnblogs.com/TendToBigData/p/10501251.html
Copyright © 2011-2022 走看看