Removing .DS_Store files from mac folders
Ben Cartwright
bencvt at gmail.com
Thu Mar 2 00:12:06 EST 2006
David Pratt wrote:
> OSError: [Errno 2] No such file or directory: '.DS_Store'
Ah. You didn't mention a traceback earlier, so I assumed the code was
executing but you didn't see the file being removed.
> >> for f in file_names:
> >> current_file = os.path.basename(f)
> >> print 'Current File: %s' % current_file
> >>
> >> # Clean mac .DS_Store
> >> if current_file == '.DS_Store':
> >> print 'a DS_Store item encountered'
> >> os.remove(f)
How are you creating file_names? More importantly, does it contain a
path (either absolute or relative to the current working directory)?
If not, you need an os.path.join, e.g.:
import os
for root_path, dir_names, file_names in os.walk('.'):
# file_names as generated by os.walk contains file
# names only (no path)
for f in file_names:
if f == '.DS_Store':
full_path = os.path.join(root_path, f)
os.remove(full_path)
--Ben
More information about the Python-list
mailing list