My apologies Ben. I should have included the traceback in my message.
The last line of the traceback I get from python when it gets to
os.remove is
OSError: [Errno 2] No such file or directory: '.DS_Store'
The traceback occurs immediately after printing:
Current File: .DS_Store
a DS_Store item encountered
so it seems that python can see the file to recognize it as the
current_file while walking the heirarchy but it cannot see the hidden
file to remove it with os.remove().
On mac recreating .DS_Store files, my original solution works to remove
them. In my code I cd to the folder and do the following which will find
and eliminate them all to any depth.
system('find . -name .DS_Store -exec rm {} \;')
I have been using this method for some time when I put together code in
a tarball since with mac the .DS_Store pollutes your files. Since I am
needing to do other things with the files, I though I would do remove
while walking the folders instead. I can always go back to this but I am
hoping someone can advise a way of deleting a hidden file. I am admin
on the mac so permissions is not an issue.
Regards,
David
Ben Cartwright wrote:
David Pratt wrote:
Hi Ben. Sorry about the cut and paste job into my email. It is part of a
larger script. It is actually all tabbed. This will give you a better idea:
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)
I'm no Mac expert, but could it be that OSX is recreating .DS_Store?
Try putting this above your os.remove call:
import os.stat
print 'Last modified:', os.stat(f)[ST_MTIME]
Then run your script a few times and see if the modified times are
different.
You might also try verifying that you get an exception when attempting
to open the file right after removing it.
--Ben