Applications of Python

4565 ワード

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("" % 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 
#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 "

転載先:https://www.cnblogs.com/TendToBigData/p/10501251.html