dylan123

通过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(\'您输入的命令无效\')

 

分类:

技术点:

相关文章:

  • 2021-12-16
  • 2022-12-23
  • 2021-12-28
  • 2022-02-01
  • 2021-12-28
猜你喜欢
  • 2022-12-23
  • 2021-08-06
  • 2021-12-18
  • 2022-12-23
  • 2021-12-04
  • 2021-12-06
  • 2021-12-16
相关资源
相似解决方案