Python 3.x Python3 subprocess.Popen无法删除带有“0”的文件;FileNotFoundError:[Errno 2]没有这样的文件或目录";错误,但文件实际存在

Python 3.x Python3 subprocess.Popen无法删除带有“0”的文件;FileNotFoundError:[Errno 2]没有这样的文件或目录";错误,但文件实际存在,python-3.x,subprocess,Python 3.x,Subprocess,Python3 subprocess.Popen无法删除文件,出现“FileNotFoundError:[Errno 2]无此类文件或目录”错误但是直接从bash终端执行命令会删除文件。 当前文件夹内容: tmp]$ ll total 16 -rw-rw-r--. 1 root root 688 Apr 29 09:28 t1.py -rw-rw-r--. 1 root root 688 Apr 29 10:41 t2.py -rw-rw-r--. 1 root root 1052 Apr

Python3 subprocess.Popen无法删除文件,出现“FileNotFoundError:[Errno 2]无此类文件或目录”错误但是直接从bash终端执行命令会删除文件。

当前文件夹内容:

tmp]$ ll
total 16
-rw-rw-r--. 1 root root  688 Apr 29 09:28 t1.py
-rw-rw-r--. 1 root root  688 Apr 29 10:41 t2.py
-rw-rw-r--. 1 root root 1052 Apr 29 10:41 t3.py
-rw-rw-r--. 1 root root  364 Apr 29 10:45 t4.py
  1 import subprocess
  2
  3
  4 def execute_shell_command(command_list):
  5     data = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  6     output = data.communicate()
  7     return output
  8
  9
 10 cmd = ["rm -rf ", "", "t2*"]
 11 command = cmd[0] + cmd[1] + cmd[2]
 12 prnt1 = "Executing command: " + command
 13 print(f"{prnt1}")
 14 out2 = execute_shell_command(command)
 15
tmp]$ python3 t4.py
Executing command: rm -rf t2*
Traceback (most recent call last):
  File "t4.py", line 14, in <module>
    out2 = execute_shell_command(command)
  File "t4.py", line 5, in execute_shell_command
    data = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "/usr/lib64/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/usr/lib64/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'rm -rf t2*': 'rm -rf t2*'
  1 import subprocess
  2
  3
  4 def execute_shell_command(command_list):
  5     data = subprocess.Popen(command_list, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  6     output = data.communicate()
  7     return output
  8
  9
 10 cmd = ["rm -rf t2*"]
 11 command = cmd[0]
 12 prnt1 = "Executing command: " + command
 13 print(f"{prnt1}")
 14 out2 = execute_shell_command(command)
 15 print(f"output = {out2[0]}")
 16 print(f"error = {out2[1]}")
Python代码(t4.py):

tmp]$ ll
total 16
-rw-rw-r--. 1 root root  688 Apr 29 09:28 t1.py
-rw-rw-r--. 1 root root  688 Apr 29 10:41 t2.py
-rw-rw-r--. 1 root root 1052 Apr 29 10:41 t3.py
-rw-rw-r--. 1 root root  364 Apr 29 10:45 t4.py
  1 import subprocess
  2
  3
  4 def execute_shell_command(command_list):
  5     data = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  6     output = data.communicate()
  7     return output
  8
  9
 10 cmd = ["rm -rf ", "", "t2*"]
 11 command = cmd[0] + cmd[1] + cmd[2]
 12 prnt1 = "Executing command: " + command
 13 print(f"{prnt1}")
 14 out2 = execute_shell_command(command)
 15
tmp]$ python3 t4.py
Executing command: rm -rf t2*
Traceback (most recent call last):
  File "t4.py", line 14, in <module>
    out2 = execute_shell_command(command)
  File "t4.py", line 5, in execute_shell_command
    data = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "/usr/lib64/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/usr/lib64/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'rm -rf t2*': 'rm -rf t2*'
  1 import subprocess
  2
  3
  4 def execute_shell_command(command_list):
  5     data = subprocess.Popen(command_list, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  6     output = data.communicate()
  7     return output
  8
  9
 10 cmd = ["rm -rf t2*"]
 11 command = cmd[0]
 12 prnt1 = "Executing command: " + command
 13 print(f"{prnt1}")
 14 out2 = execute_shell_command(command)
 15 print(f"output = {out2[0]}")
 16 print(f"error = {out2[1]}")
输出:

tmp]$ ll
total 16
-rw-rw-r--. 1 root root  688 Apr 29 09:28 t1.py
-rw-rw-r--. 1 root root  688 Apr 29 10:41 t2.py
-rw-rw-r--. 1 root root 1052 Apr 29 10:41 t3.py
-rw-rw-r--. 1 root root  364 Apr 29 10:45 t4.py
  1 import subprocess
  2
  3
  4 def execute_shell_command(command_list):
  5     data = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  6     output = data.communicate()
  7     return output
  8
  9
 10 cmd = ["rm -rf ", "", "t2*"]
 11 command = cmd[0] + cmd[1] + cmd[2]
 12 prnt1 = "Executing command: " + command
 13 print(f"{prnt1}")
 14 out2 = execute_shell_command(command)
 15
tmp]$ python3 t4.py
Executing command: rm -rf t2*
Traceback (most recent call last):
  File "t4.py", line 14, in <module>
    out2 = execute_shell_command(command)
  File "t4.py", line 5, in execute_shell_command
    data = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "/usr/lib64/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/usr/lib64/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'rm -rf t2*': 'rm -rf t2*'
  1 import subprocess
  2
  3
  4 def execute_shell_command(command_list):
  5     data = subprocess.Popen(command_list, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  6     output = data.communicate()
  7     return output
  8
  9
 10 cmd = ["rm -rf t2*"]
 11 command = cmd[0]
 12 prnt1 = "Executing command: " + command
 13 print(f"{prnt1}")
 14 out2 = execute_shell_command(command)
 15 print(f"output = {out2[0]}")
 16 print(f"error = {out2[1]}")
tmp]$python3 t4.py
正在执行命令:rm-rft2*
回溯(最近一次呼叫最后一次):
文件“t4.py”,第14行,在
out2=执行外壳命令(命令)
文件“t4.py”,第5行,在execute_shell_命令中
data=subprocess.Popen(命令列表,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
文件“/usr/lib64/python3.6/subprocess.py”,第729行,在__
恢复信号,启动新会话)
文件“/usr/lib64/python3.6/subprocess.py”,第1364行,在执行子进程中
引发子项异常类型(错误号、错误消息、错误文件名)
FileNotFoundError:[Errno 2]没有这样的文件或目录:“rm-rf t2*”:“rm-rf t2*”

以下代码已生效:

tmp]$ ll
total 16
-rw-rw-r--. 1 root root  688 Apr 29 09:28 t1.py
-rw-rw-r--. 1 root root  688 Apr 29 10:41 t2.py
-rw-rw-r--. 1 root root 1052 Apr 29 10:41 t3.py
-rw-rw-r--. 1 root root  364 Apr 29 10:45 t4.py
  1 import subprocess
  2
  3
  4 def execute_shell_command(command_list):
  5     data = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  6     output = data.communicate()
  7     return output
  8
  9
 10 cmd = ["rm -rf ", "", "t2*"]
 11 command = cmd[0] + cmd[1] + cmd[2]
 12 prnt1 = "Executing command: " + command
 13 print(f"{prnt1}")
 14 out2 = execute_shell_command(command)
 15
tmp]$ python3 t4.py
Executing command: rm -rf t2*
Traceback (most recent call last):
  File "t4.py", line 14, in <module>
    out2 = execute_shell_command(command)
  File "t4.py", line 5, in execute_shell_command
    data = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "/usr/lib64/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/usr/lib64/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'rm -rf t2*': 'rm -rf t2*'
  1 import subprocess
  2
  3
  4 def execute_shell_command(command_list):
  5     data = subprocess.Popen(command_list, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  6     output = data.communicate()
  7     return output
  8
  9
 10 cmd = ["rm -rf t2*"]
 11 command = cmd[0]
 12 prnt1 = "Executing command: " + command
 13 print(f"{prnt1}")
 14 out2 = execute_shell_command(command)
 15 print(f"output = {out2[0]}")
 16 print(f"error = {out2[1]}")

基本上,按原样编写命令,然后在subprocess.Popen中添加一个参数shell=True

使用
cmd=[“rm”、“-rf”、“t2*”]
哦,是的,你是对的,每个用逗号分隔的值都应该是命令列表中的不同元素!这个解决方案也没有帮助。不知道为什么!我所做的更改是:
cmd=[“rm”、“-rf”、“t2*”]
错误:
FileNotFoundError:[Errno 2]没有这样的文件或目录:“rm rf t2*”:“rm rf t2*”
此解决方案也没有帮助。不知道为什么!我所做的更改是:
cmd=[“rm”、“-rf”、“t2*”]
错误:
FileNotFoundError:[Errno 2]没有这样的文件或目录:'rm-rft2*':'rm-rft2*'