您的位置:首页 > 移动开发

Applications of Python

2017-04-04 16:23 197 查看
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 "
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: