Removing .DS_Store files from mac folders

David Pratt fairwinds at eastlink.ca
Thu Mar 2 08:12:58 EST 2006


Hi Ben. I hadn't realize that walk was just giving the file name so the 
join did the job just great. Many thanks for helping me out with this.

Regards,
David

Ben Cartwright wrote:
> 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