使用 python subprocess.call rm 目录下的所有文件
rm all files under a directory using python subprocess.call
我正在编写一个脚本,将文件移动到用户主文件夹中的 .trash 目录中。我想通过使用 python 的 subprocess.call()
调用 rm -rf /home/user/.trash/*
添加清空垃圾目录的功能
~$ touch file1
~$ trash file1
['mv', 'file1', '/home/rodney/.trash/']
~$ ls .trash
file1
~$ trash --empty
['rm', '-rf', '/home/rodney/.trash/*']
~$ ls .trash
file1
如您所见,rm 命令没有删除垃圾箱中的内容。但是,如果我直接在命令行上执行命令,它就可以工作。
~$ rm -rf /home/rodney/.trash/*
~$ ls .trash
~$
输出来自以下代码
print(cmd)
subprocess.call(cmd)
奇怪的是,如果我从 cmd 列表的最后一个参数中排除 *,那么子进程调用会起作用,但也会删除整个 .trash 目录。我不想删除 .trash 目录;只有它下面的所有内容。
总结一下问题
这个有效
import subprocess
subprocess.call(['rm', '-rf', '/home/rodney/.trash/'])
这不
import subprocess
subprocess.call(['rm', '-rf', '/home/rodney/.trash/*'])
为什么?
Shell 将 *
扩展为文件名。您需要传递 shell=True
关键字参数,以便 shell 将解释 *
.
import subprocess
subprocess.call('rm -rf /home/rodney/.trash/*', shell=True)
根据subprocess
- Frequently Used Arguments:
If shell
is True, the specified command will be executed through the
shell. This can be useful if you are using Python primarily for the
enhanced control flow it offers over most system shells and still want
convenient access to other shell features such as shell pipes,
filename wildcards, environment variable expansion, and expansion of ~
to a user’s home directory. However, note that Python itself offers
implementations of many shell-like features (in particular, glob,
fnmatch, os.walk(), os.path.expandvars(), os.path.expanduser(), and
shutil).
不要shell出来。
这使用 glob.glob()
to identify the files to be removed, and shutil.rmtree()
删除子目录。
#!/usr/bin/env python
import os, os.path
import glob
import shutil
def remove_thing(path):
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
def empty_directory(path):
for i in glob.glob(os.path.join(path, '*')):
remove_thing(i)
empty_directory('trash')
示例:
$ tree trash/
trash/
├── aaa
├── bbb
├── ccc
├── ddd
└── sub
├── iii
└── jjj
1 directory, 6 files
$ ./go.py
$ tree trash/
trash/
0 directories, 0 files
我正在编写一个脚本,将文件移动到用户主文件夹中的 .trash 目录中。我想通过使用 python 的 subprocess.call()
调用rm -rf /home/user/.trash/*
添加清空垃圾目录的功能
~$ touch file1
~$ trash file1
['mv', 'file1', '/home/rodney/.trash/']
~$ ls .trash
file1
~$ trash --empty
['rm', '-rf', '/home/rodney/.trash/*']
~$ ls .trash
file1
如您所见,rm 命令没有删除垃圾箱中的内容。但是,如果我直接在命令行上执行命令,它就可以工作。
~$ rm -rf /home/rodney/.trash/*
~$ ls .trash
~$
输出来自以下代码
print(cmd)
subprocess.call(cmd)
奇怪的是,如果我从 cmd 列表的最后一个参数中排除 *,那么子进程调用会起作用,但也会删除整个 .trash 目录。我不想删除 .trash 目录;只有它下面的所有内容。
总结一下问题
这个有效
import subprocess
subprocess.call(['rm', '-rf', '/home/rodney/.trash/'])
这不
import subprocess
subprocess.call(['rm', '-rf', '/home/rodney/.trash/*'])
为什么?
Shell 将 *
扩展为文件名。您需要传递 shell=True
关键字参数,以便 shell 将解释 *
.
import subprocess
subprocess.call('rm -rf /home/rodney/.trash/*', shell=True)
根据subprocess
- Frequently Used Arguments:
If
shell
is True, the specified command will be executed through the shell. This can be useful if you are using Python primarily for the enhanced control flow it offers over most system shells and still want convenient access to other shell features such as shell pipes, filename wildcards, environment variable expansion, and expansion of ~ to a user’s home directory. However, note that Python itself offers implementations of many shell-like features (in particular, glob, fnmatch, os.walk(), os.path.expandvars(), os.path.expanduser(), and shutil).
不要shell出来。
这使用 glob.glob()
to identify the files to be removed, and shutil.rmtree()
删除子目录。
#!/usr/bin/env python
import os, os.path
import glob
import shutil
def remove_thing(path):
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
def empty_directory(path):
for i in glob.glob(os.path.join(path, '*')):
remove_thing(i)
empty_directory('trash')
示例:
$ tree trash/
trash/
├── aaa
├── bbb
├── ccc
├── ddd
└── sub
├── iii
└── jjj
1 directory, 6 files
$ ./go.py
$ tree trash/
trash/
0 directories, 0 files