Python脚本循环遍历目录中的所有文件,删除任何小于200 kB的文件
我想删除文件夹中小于200 kB的所有文件Python脚本循环遍历目录中的所有文件,删除任何小于200 kB的文件,python,Python,我想删除文件夹中小于200 kB的所有文件 只是想确定一下,当我在我的macbook上执行ls-la时,文件大小是171或143,我假设这是kb正确的吗?这是目录和所有子目录: 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)
只是想确定一下,当我在我的macbook上执行ls-la时,文件大小是171或143,我假设这是kb正确的吗?这是目录和所有子目录:
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)
导入操作系统,操作系统路径
对于os.walk(dirtocheck)中的根目录文件:
对于文件中的f:
fullpath=os.path.join(根,f)
如果os.path.getsize(完整路径)<200*1024:
删除操作系统(完整路径)
或:
导入操作系统,操作系统路径
fileiter=(os.path.join(root,f)
对于os.walk(dirtocheck)中的根目录文件
(用于文件中的f)
smallfileiter=(如果os.path.getsize(f)<200*1024,则f代表文件编辑器中的f)
对于smallfileiter中的小型文件:
移除操作系统(小)
通常ls-la
以字节为单位
如果您希望它以“人类可读”的形式出现,请使用命令
ls-alh
您也可以使用find
find /path -type f -size -200k -delete
你也可以使用
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
这是一个关于python的问题,答案应该在同一个域中
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