单反同步删除RAW文件
我使用的是D750,使用jpeg+raw双存储的方式。在旅行的过程中,通过相机自带wifi,将jpeg照片同步到手机,然后就可以浏览,删除,修图,发票圈了。这样在旅行的过程中就筛选了大部分照片,回来的时候就不用那么辛苦的筛选照片了。毕竟快门一时爽,选片火葬场!但是手机只是筛选了jpeg,回到家导入电脑的是raw,这里就需要一个工具同步删除raw文件。
所以就写了一个同步删除raw文件的python脚本,脚本只在Mac上测试了,没有在其它平台测试
#!/usr/bin/env python3
# -- coding:utf-8 --
import os
import argparse
import subprocess
class FileStatus:
def __init__(self, _file, delete=0):
self.delete = delete
self.filePath = _file
self.baseName = os.path.basename(_file)
self.fileName = os.path.splitext(self.baseName)[0]
def getAllFilesInDir(path):
allFileStatus = []
for parent, dirnames, filenames in os.walk(path):
for file in filenames:
if file.startswith('.'):
continue
file_path = os.path.join(parent, file)
allFileStatus.append(FileStatus(file_path))
return allFileStatus
def getRemoteAndroidJpegFiles(path):
out_bytes = subprocess.check_output(['adb', 'shell', 'ls', path])
out_string = out_bytes.decode("utf-8")
allFilesList = out_string.splitlines()
allFileStatus = []
for file in allFilesList:
ext = os.path.splitext(file)[1].lower()
if ext == '.jpeg' or ext == '.jpg':
if file.startswith('.'):
continue
file_path = os.path.join(path, file)
allFileStatus.append(FileStatus(file_path))
return allFileStatus
def parseArg():
parser = argparse.ArgumentParser(description="与Jpeg文件夹同步,将Jpeg文件夹中已经删除的图片所对应的Raw文件也删除。注意本脚本不考虑扩展名,需要你保证jpegDir"
"中只有jpeg文件,rawDir中只有raw文件")
parser.add_argument('-j', '--jpegDir', nargs=1, type=str, help="JPEG文件目录")
parser.add_argument('-a', '--androidDir', nargs=1, type=str, help="Android JPEG文件目录")
parser.add_argument('-r', '--rawDir', nargs=1, type=str, help="RAW文件目录,将会与JPEG目录比较文件名(不包含扩展名)")
parser.add_argument('-d', '--delete', action='store_true', help="执行删除操作。如果不加此选项,将不执行删除操作")
return parser.parse_args()
def main():
args = parseArg()
if args.androidDir is not None:
jpegDir = args.androidDir[0]
else:
jpegDir = args.jpegDir[0]
if not os.path.exists(jpegDir):
print('source directory not exists!')
exit(1)
if not os.path.isdir(jpegDir):
print('source directory is not a directory!')
exit(1)
rawDir = args.rawDir[0] # 需要删除的文件夹
if not os.path.exists(rawDir):
print('target directory not exists!')
exit(1)
if not os.path.isdir(rawDir):
print('target directory is not a directory!')
exit(1)
if args.androidDir is not None:
allJpegFiles = getRemoteAndroidJpegFiles(jpegDir)
else:
allJpegFiles = getAllFilesInDir(jpegDir)
allRawFiles = getAllFilesInDir(rawDir)
for rawFile in allRawFiles:
isDelete = True
for jpegFile in allJpegFiles:
if jpegFile.fileName == rawFile.fileName:
isDelete = False
break
if isDelete:
rawFile.delete = 1
count = 0
for rawFile in allRawFiles:
if rawFile.delete == 1:
count += 1
# os.remove(file.file)
print("%s 删除" % rawFile.baseName)
else:
print("%s " % rawFile.baseName)
print("\nRaw 总共 %d 张, 将要删除 %d 张" % (len(allRawFiles), count))
if args.delete:
count = 0
try:
for rawFile in allRawFiles:
if rawFile.delete == 1:
os.remove(rawFile.filePath)
count += 1
finally:
print("已删除了 %d 张" % count)
if __name__ == "__main__":
main()
使用说明:
同步jpeg目录在andorid手机上,则打开手机调试功能,并在PC安装adb前提下使用-a
选项指定手机Jpeg目录
- 预览删除操作
SyncDeleteRawFiles.py -a /sdcard/DCIM/NIKON -r "/Volumes/NIKON D750/DCIM/106ND750"
- 执行删除操作
SyncDeleteRawFiles.py -a /sdcard/DCIM/NIKON -r "/Volumes/NIKON D750/DCIM/106ND750" -d
同步Jpeg目录在本机上,则使用-j
选项执行Jpeg目录
- 预览删除操作
SyncDeleteRawFiles.py -j /Users/kevin/Pictures/亚庇照片 -r "/Volumes/NIKON D750/DCIM/106ND750"
- 执行删除操作
SyncDeleteRawFiles.py -j /Users/kevin/Pictures/亚庇照片 -r "/Volumes/NIKON D750/DCIM/106ND750" -d
使用示例
/Users/kevin/WorkSpace/pythonWorkspace/Python/SyncDeleteRawFiles.py -a /sdcard/DCIM/NIKON -r "/Volumes/NIKON D750/DCIM/106ND750"
DSC_4974.NEF
DSC_4975.NEF
DSC_4976.NEF
DSC_4977.NEF
DSC_4978.NEF
DSC_4979.NEF
....
DSC_4983.NEF
DSC_4984.NEF
DSC_4985.NEF
DSC_4988.NEF 删除
DSC_4989.NEF 删除
DSC_4990.NEF 删除
DSC_4991.NEF 删除
DSC_4992.NEF 删除
DSC_4993.NEF 删除
DSC_4994.NEF 删除
DSC_4996.NEF 删除
DSC_4997.NEF 删除
DSC_4998.NEF 删除
DSC_4999.NEF 删除
DSC_5000.NEF 删除
DSC_5001.NEF 删除
DSC_5002.NEF 删除
DSC_5003.NEF 删除
DSC_5004.NEF 删除
DSC_5005.NEF 删除
DSC_5006.NEF 删除
DSC_5007.NEF 删除
DSC_5008.NEF 删除
DSC_5009.NEF 删除
Raw 总共 327 张, 将要删除 21 张