Google python copyspecial.py(python 2.7)

#!/usr/bin/python

import sys
import re
import os
import shutil
import commands
import zipfile

"""Copy Special exercise
"""

# +++your code here+++
# Write functions and modify main() to call them

def get_specials(dir):
com='ls '+ dir
status,output=commands.getstatusoutput(com)
specials=[]
for special in output.split():
if re.findall(r'__(\w+)__',special):
specials.append(special)
return specials

def to_dir(dir,srcs):
if not os.path.exists(dir):
os.mkdir(dir)
for src in srcs:
shutil.copy(src,dir)

#s1:
def to_zip(zf,srcs):
f=zipfile.ZipFile(zf,'w')
for src in srcs:
f.write(src)
f.close()

#s2
def to_zip(zf,srcs):

com='zip '+ zf +' '+' '.join(srcs)
print com
status,output=commands.getstatusoutput(com)
if not status:
print 'zip successed!'

def main():
# This basic command line argument parsing code is provided.
# Add code to call your functions below.

# Make a list of command line arguments, omitting the [0] element
# which is the script itself.
args = sys.argv[1:]
if not args:
print "usage: [--todir dir][--tozip zipfile] dir [dir ...]";
sys.exit(1)

# todir and tozip are either set from command line
# or left as the empty string.
# The args array is left just containing the dirs.
todir = ''
if args[0] == '--todir':
todir = args[1]
del args[0:2]

tozip = ''
if args[0] == '--tozip':
tozip = args[1]
del args[0:2]

if len(args) == 0:
print "error: must specify one or more dirs"
sys.exit(1)

specials=get_specials(args[0])
if todir:
to_dir(todir,specials)
if tozip:
to_zip(tozip,specials)

if __name__ == "__main__":
main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值