美文网首页
python执行系统命令操作文件

python执行系统命令操作文件

作者: 打五笔的程序员 | 来源:发表于2021-04-16 09:52 被阅读0次
在工作中经常要使用到一些文件操作的方法,比如复制,剪切,删除文件或文件夹等,python中有os,shutil方法,还可以执行系统命令方法,比如windows 的copy,move ,rmdir,linux的cp,mv,rmdir等等命令

1.判断系统,执行对应的命令

import os
from subprocess import run
from platform import platform #导入获取操作系统信息的方法


src = r"E:\test\data"
dst = r"E:\test\data_result"


if "Windows" in platform():
    cmd = "xcopy /e {} {}".format(src,dst) #复制目data录下面的所有文件跟文件夹到data_result目录
    run(cmd,shell=True)
if "Linux" in platform():
    cmd = "cp {}/* {}".format(src,dst)  #复制目data录下面的所有文件跟文件夹到data_result目录
    run(cmd,shell=True)

目标
结果

系统命令都有很多参数可选择,具体需要什么,就选什么参数

相关文章

网友评论

      本文标题:python执行系统命令操作文件

      本文链接:https://www.haomeiwen.com/subject/mabflltx.html