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 \n" % 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:\Python27\lib\ntpath.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 "

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值