我想删除文件夹中大小小于200 kB的所有文件.
只是想在这里确定,当我在我的macbook上执行ls -la时,文件大小为171或143,我假设这是kb正确吗?
hug*_*own 58
这会执行目录和所有子目录:
import os, os.path
for root, _, files in os.walk(dirtocheck):
for f in files:
fullpath = os.path.join(root, f)
if os.path.getsize(fullpath) < 200 * 1024:
os.remove(fullpath)
Run Code Online (Sandbox Code Playgroud)
要么:
import os, os.path
fileiter = (os.path.join(root, f)
for root, _, files in os.walk(dirtocheck)
for f in files)
smallfileiter = (f for f in fileiter if os.path.getsize(f) < 200 * 1024)
for small in smallfileiter:
os.remove(small)
Run Code Online (Sandbox Code Playgroud)
gho*_*g74 31
你也可以用 find
find /path -type f -size -200k -delete
Run Code Online (Sandbox Code Playgroud)
The*_*eer 28
你也可以用
import os
files_in_dir = os.listdir(path_to_dir)
for file_in_dir in files_in_dir:
#do the check you need on each file
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
51033 次 |
最近记录: |