Skip to content

Instantly share code, notes, and snippets.

@jhezjkp
Created March 14, 2018 06:54
Show Gist options
  • Save jhezjkp/53cfb30dd66c09f66020a92b73c41373 to your computer and use it in GitHub Desktop.
Save jhezjkp/53cfb30dd66c09f66020a92b73c41373 to your computer and use it in GitHub Desktop.
移除几个目录下同名但size小的文件
#!/usr/bin/env python
#encoding=utf-8
import os
import os.path
"""
指定目录下只留下最大的文件,小一点的复制文件删除
"""
def keep_max(paths):
size_map = {}
for path in paths:
for filename in os.listdir(path):
file_path = os.path.join(path, filename)
size = os.stat(file_path).st_size
# print file_path, filename, size
if filename in size_map:
fp, s = size_map.get(filename)
if s > size:
print 'sfuck', file_path, fp, size, s
os.remove(file_path)
elif s == size:
print "same", file_path, fp, size
os.remove(file_path)
else:
print 'lfuck', file_path, fp, size, s
os.remove(fp)
size_map[filename] = (file_path, size)
else:
size_map[filename] = (file_path, size)
if __name__ == "__main__":
paths = ["/Users/vivia/photo/image1/", "/Users/vivia/photo/image2/", "/Users/vivia/photo/image3/"]
keep_max(paths)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment