python 实现cp命令 复制文件和文件夹

随手写了个复制命令
在linux下可以直接./ 运行哟,windows下应该要删掉第一行,然后 python3 XXX.py

#!/usr/bin/env python3
import sys,getopt,shutil

def copyfile(infile,outfile):
    try:
        shutil.copy(infile,outfile)
    except:
        print('''Can't open this file''')
        return

def copydir(indir,outdir):
    try:
        shutil.copytree(indir,outdir)
    except:
        print('This dir is wrong')

def main():
    if len(sys.argv) < 2:
        print("NO action specified")
    else:
        if sys.argv[1].startswith('--'):
            if sys.argv[1][2:] == 'version':
                print("version 0.1")
            if sys.argv[1][2:] == 'help':
                print('''
                    This program prints files to standard output.
                    Any number of files can be specified.
                    Options include:
                    -- version : Prints the version number
                    -- help : Display this help
                    - i : add input file
                    - o : add output file
                ''')
            else:
                print("Unknow option.")
        else:
            opts,args = getopt.getopt(sys.argv[1:],"i:o:")
            input_file,output_file,input_dir,output_dir = '','','',''
            for op,value in opts:
                if op == "-i" :
                    if '.' in value:
                        input_file = value
                    else:
                        input_dir = value
                elif op == "-o":
                    if '.' in value:
                        output_file = value
                    else:
                        output_dir = value
            if input_file and output_file:
                copyfile(input_file,output_file)
            elif input_dir and output_dir:
                copydir(input_dir,output_dir)
            else:
                print('Input Wrong')
    
        
if __name__ == '__main__':
    main()

sys.argv

getopt

shutil

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容