Python 执行RD命令的问题汇总(无效开关 - “Image“。)

简介: Python 执行RD命令的问题汇总(无效开关 - “Image“。)

1、起因

删除文件夹的时经常无法删除

2、操作环境

pycharm 2020 社区版

python 3.7 (内置TK和OS)

3、代码

UI代码我一会贴,先看看python执行dos命令的语句。

def implement(self):
        result = tkinter.messagebox.askokcancel(title='删除提示', message='删除后将丢失文件夹内的所有文件确定执行?')
        if str(result).lower()=="true":
            file_path=self.txt_root_path.GetValue() # 获取文件夹
            cmd="rd /s/q {0}".format(file_path) #dos命令
            print(cmd)
            exe_result= os.system(cmd)
            print(exe_result)
        #AdenMessageBox.show("implement成功") rd /s/q "D:/Image/TT/ASP8.png"
        pass

就这么一段代码搞出来好几几个问题。

第一乱码。

dos命令执行报错了。但是控制台乱码,因为windows的cmd默认编码是gbk因此得将pycharm编码改成gbk。

之前讲过如何修改。

修改后

不乱码了但是报错。 不乱码了但是报错。 无效开关 - "Image"。

Z这个需要将 路径上加双引号。

rd /s/q "D:/Image/TT/ASP8.png"

这个是我第一次的操作结果无法删除。因为rd不支持删除文件。

UI代码:

import os
import tkinter
from tkinter import messagebox, filedialog
from framework.access.system.SysConfigAccess import SysConfigAccess
from framework.base.BaseFrame import BaseFrame
from framework.control.AdenButton import AdenButton
from framework.control.AdenCheckButton import AdenCheckButton
from framework.control.AdenEntry import AdenEntry
from framework.control.AdenLabel import AdenLabel
from framework.control.AdenMessageBox import AdenMessageBox
from framework.control.AdenTkPlugin import TkPlugin
from framework.model.system.SysConfigEntity import SysConfigEntity
from framework.pulgin.Tools import Tools
class ForceDeleteView(BaseFrame):
    def __init__(self):
        pass
    def getFrame(self, root, notebook):
        self.note_book = notebook
        self.frame = tkinter.Frame(root)
        self.frame.configure(background=TkPlugin.background())
        self.frame.columnconfigure(1, weight=1)  # 设置第2列自动适应Frame 需要sitcky布局配合
        self.frame.rowconfigure(8, weight=1)
        # 第1行
        # lable_sys_name = AdenLabel(self.frame, text="系统名称:")
        # lable_sys_name.grid(row=0, column=0, sticky=tkinter.E)
        # self.txt_sys_name = AdenEntry(self.frame)  # 邮件名称 通过自定义控件实现 读取值和获取值都可以通过控件获取
        # self.txt_sys_name.grid(row=0, column=1, sticky=TkPlugin.sticky_all(), padx="6", pady="6", columnspan=2)
        # 第1行
        label_mail_smtp_name = AdenLabel(self.frame, text="删除路径或文件:", bg=TkPlugin.background())
        label_mail_smtp_name.grid(row=0, column=0, sticky=tkinter.E)
        self.txt_root_path = AdenEntry(self.frame)
        self.txt_root_path.grid(row=0, column=1, sticky=TkPlugin.sticky_all(), padx="6", pady="6")
        btn_choose_path = AdenButton(self.frame, text="选择路径", width=8, command=lambda: self.choose_root_path("path"))
        btn_choose_path.grid(row=0, column=2, sticky=tkinter.W,  padx="2")
        # btn_choose_file = AdenButton(self.frame, text="选择文件", width=8, command=lambda: self.choose_root_path("file"))
        # btn_choose_file.grid(row=0, column=3, sticky=tkinter.W, padx="2")
        #最后一行也是第9行按钮操作
        btnSave = AdenButton(self.frame, text="执行", width=8, command=lambda: self.implement())
        btnSave.grid(row=9, column=0, sticky=tkinter.E, columnspan="2")
        btnDelete = AdenButton(self.frame, text="关闭窗体", bg="#D9534F", width=8, command=lambda: self.forget())
        btnDelete.grid(row=9, column=2, sticky=tkinter.E)
        return self.frame
    def choose_root_path(self,type):
        if type=="path":
            folderpath = filedialog.askdirectory()
            self.txt_root_path.SetValue(folderpath)
        else:
            file = filedialog.askopenfilename()
            self.txt_root_path.SetValue(file)
        pass
    def implement(self):
        result = tkinter.messagebox.askokcancel(title='删除提示', message='删除后将丢失文件夹内的所有文件确定执行?')
        if str(result).lower()=="true":
            file_path=self.txt_root_path.GetValue() # 获取文件夹
            cmd="rd /s/q {0}".format(file_path) #dos命令
            print(cmd)
            exe_result= os.system(cmd)
            print(exe_result)
        #AdenMessageBox.show("implement成功") rd /s/q "D:/Image/TT/ASP8.png"
        pass
    def forget(self):
        self.note_book.forget(self.frame)
        pass

界面:

 

目录
相关文章
|
8天前
|
机器学习/深度学习 Shell 开发工具
Python使用管道执行git命令报错|4-7
Python使用管道执行git命令报错|4-7
|
21天前
|
Unix Shell Linux
nohup python -u ai_miniprogram_main.py > ../iwork.out 2>&1 & 这句命令是做什么的?
nohup python -u ai_miniprogram_main.py > ../iwork.out 2>&1 & 这句命令是做什么的?
9 1
|
6天前
|
Python Windows
Python:执行py命令,提示: Can‘t find a default Python.
Python:执行py命令,提示: Can‘t find a default Python.
|
10天前
|
Shell Linux Python
python执行linux系统命令的几种方法(python3经典编程案例)
文章介绍了多种使用Python执行Linux系统命令的方法,包括使用os模块的不同函数以及subprocess模块来调用shell命令并处理其输出。
13 0
|
2月前
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
|
2月前
|
前端开发 计算机视觉
Building wheel for opencv-python (pyproject.toml) ,安装命令增加 --verbose 参数
Building wheel for opencv-python (pyproject.toml) ,安装命令增加 --verbose 参数
117 2
|
2月前
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
Azure 容器实例(Azure Container Instances,简称 ACI)是一个无服务器容器解决方案,允许用户在 Azure 云环境中运行 Docker 容器,而无需设置虚拟机、集群或编排器。 ACI 适用于任何可以在隔离容器中操作的场景,包括事件驱动的应用程序、从容器开发管道快速部署、数据处理和生成作业。
|
2月前
|
Linux Shell 数据库
python Django教程 之 安装、基本命令、视图与网站
python Django教程 之 安装、基本命令、视图与网站
|
3月前
|
Linux iOS开发 MacOS
python的virtualenv虚拟环境常见问题和命令
`venv`是Python的内置模块,用于创建隔离的虚拟环境。创建虚拟环境如`python3 -m venv myenv`,激活环境在Windows上是`./venv/Scripts/activate`,在Unix-like系统是`source myenv/bin/activate`。退出环境用`deactivate`。`pip list`查看已安装包,`pip install`安装包,`pip freeze > requirements.txt`保存依赖。PyCharm中红色`venv`表示项目使用了虚拟环境。
69 2
 python的virtualenv虚拟环境常见问题和命令
|
3月前
|
Python
python常用命令
python常用命令
32 1
下一篇
无影云桌面