A python script that deletes ._DS_Store and ._ * files created on Mac

use

If you connect a portable HDD or USB memory to a Mac and then connect to Windows or Linux, I notice that a lot of ._ [existing file names] like .DS_Store and metafiles of existing files are created. These files are smaller than the sector size of the file system, so thousands of files can waste a few GB. That was exactly what it was.

On Linux, you can delete it with ``` find. -Name" ._ * "-exec rm -f {} ;` `` Even if I try to search for ._ * with Windows explorer and delete it, it is very difficult to delete because ordinary files and directories that are not ._ * are caught.

So I tried to make a script with python easily for those who are in trouble and myself. In addition, since we do not perform strict file checks, there is a possibility that files that should not be deleted will be deleted, so please use this script at your own risk.

import os
import sys
import shutil

def deldirs(baseDir):
  print "Start " + baseDir
  ds = [baseDir]
  while True:
    if len(ds) == 0:
      print "Finished!!"
      break
    # Pop next target dir
    d = ds.pop(0)
    # Delete file
    fs = os.listdir(d)
    for fn in fs:
      fp = d + "/" +fn
      if fn[:2] == "._" or fn == ".DS_Store":
        if os.path.isfile(fp):
          print "Del File " + fp
          os.remove(fp)
      #elif os.path.isdir(fp):
      #    print "Del Dir " + fp
      #    shutil.rmtree(fp)

if __name__ == "__main__":
  if len(sys.argv) != 2:
    print "Usage: delmacfile.py TARGET_DIR"
    exit(1)
  baseDir = sys.argv[1]
  if os.path.isdir(baseDir) == False:
    print "Please arg1 is a TAGET_DIR"
    exit(2)
  deldirs(baseDir)

There seems to be a way to do it with Windows commands, but it is for Python lovers.

Recommended Posts

A python script that deletes ._DS_Store and ._ * files created on Mac
A python script for Mac that zips without garbled characters on Windows
[Python] I made a script that automatically cuts and pastes files on a local PC to an external SSD.
A python client application that downloads and deletes files from S3 by specifying a bucket
Building a Python environment on a Mac and using Jupyter lab
A set of script files that do wordcloud in Python3
Building a Python environment on Mac
Create a Python environment on Mac (2017/4)
Build a Python environment on your Mac with Anaconda and PyCharm
Create a python environment on your Mac
A memo with Python2.7 and Python3 on CentOS
Notes on building Python and pyenv on Mac
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
python on mac
A nice nimporter that connects nim and python
Build a Python environment on Mac (Mountain Lion)
Build a Python development environment on your Mac
"Python Kit" that calls a Python script from Swift
Try importing MLB data on Mac and Python
[Python, PyPDF2] A script that divides a spread PDF into two left and right
A Python script that reads a SQL file, executes BigQuery and saves the csv
A Python script that crawls RSS in Azure Status and posts it to Hipchat
[Grasshopper] When creating a data tree on Python script
Install Python3 on Mac and build environment [Definitive Edition]
Install selenium on Mac and try it with python
Install Python on Mac
Install Python 3 on Mac
Python> Implementation that synthesizes 3 files with variable columns and outputs 3 items on each line
A script that combines your favorite python modules and binaries into one Lambda Layer
What's in that variable (when running a Python script)
Created a Python library DateTimeRange that handles time ranges
Build a Python environment on your Mac using pyenv
Installing Python 3 on Mac and checking basic operation Part 1
[Python] A notebook that translates and downloads the ipynb file on GitHub into Japanese.
problem that certificate verify failed on mac python pip
Install Python 3.4 on Mac
Create a decent shell and python environment on Windows
A python script that wants to use Mac startup / end time for attendance management
"Stop committing Japanese files to git on Mac> <" For the time being, I wrote a script to search for incompatible Japanese files on Mac and Linux.
A shell script that numbers duplicate names when creating files
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
A Python script that saves a clipboard (GTK) image to a file.
[Python] Create a linebot that draws any date on a photo
Let's create a script that registers with Ideone.com in Python.
Creating a Python script that supports the e-Stat API (ver.2)
Create code that outputs "A and pretending B" in python
I created a class in Python and tried duck typing
Build a Python extension for E-Cell 4 on Mac OSX (Yosemite)
Create a Python script for Wake on LAN (NAT traversal Wake on LAN [5])
How to write a metaclass that supports both python2 and python3
Create a virtual environment for python on mac [Very easy]
A python script that converts Oracle Database data to csv
A Python script that compares the contents of two directories
Python on Ruby and angry Ruby on Python
Install pygame on python3.4 on mac
Handling of python on mac
Install pandas 0.14 on python3.4 [on Mac]
Notes on installing Python on Mac
Catalina on Mac and pyenv
[Python] Draw elevation data on a sphere with Plotly and draw a globe that can be rotated round and round
How to make Python 3.x and 2.x coexist on Mac (I also included opencv as a bonus)