Python脚本实现在cmd执行相关命令

通过Python脚本实现,在cmd命令执行文件的cp(复制)、rm(删除)、rename(重命名)、move(文件移动)、mkdir(创建目录)

cmd执行命令格式:python xxx.py 用户名 密码 cp 文件路径 目的地址

cmd命令:

 

python D:\python_22\cmd.py dylan 123 cp D:\python_22\code\asw.py D:\python_22\cd
python D:\python_22\cmd.py dylan 123 rm D:\python_22\day23\code
python D:\python_22\cmd.py dylan 123 rename D:\python_22\re D:\python_22\rem
python D:\python_22\cmd.py dylan 123 move D:\python_22\code\文件 D:\python_22\cd
python D:\python_22\cmd.py dylan 123 mkdir D:\python_22\新的文件夹名

 

Python实现脚本:

 

import os
import sys
import shutil
if len(sys.argv) >= 5:
    if sys.argv[1] =='dylan' and sys.argv[2] == '123':
        if sys.argv[3] == 'cp' and len(sys.argv) == 6:
            if os.path.exists(sys.argv[4]) and os.path.exists(sys.argv[5]):
                filename = os.path.basename(sys.argv[4])
                path = os.path.join(sys.argv[5],filename)
                shutil.copy2(sys.argv[4],path)
        elif sys.argv[3] == 'rm' and len(sys.argv) == 5:
            if os.path.exists(sys.argv[4]):
                if os.path.isfile(sys.argv[4]):os.remove(sys.argv[4])
                else:shutil.rmtree(sys.argv[4])
        elif sys.argv[3] == 'rename'and len(sys.argv) == 6:
            if os.path.exists(sys.argv[4]):
                    os.rename(sys.argv[4],sys.argv[5])
        elif sys.argv[3] == 'move' and len(sys.argv) == 6:
            if os.path.exists(sys.argv[4]) and os.path.exists(sys.argv[5]):
                shutil.move(sys.argv[4],sys.argv[5])
        elif sys.argv[3] == 'mkdir' and len(sys.argv) == 5:
            if os.path.exists(sys.argv[4]):
                os.mkdir(sys.argv[4])
else:
    print('您输入的命令无效')

 

posted @ 2020-03-11 20:21  Dylan123  阅读(805)  评论(0编辑  收藏  举报