Removing .DS_Store files from mac folders

D

David Pratt

Hi. I'm trying to clean files for packaging on mac using os.path.walk
and want to clean the .DS_Store files that are hidden from view but
could end up in code that I produce.

# Clean mac .DS_Store
if current_file == '.DS_Store':
print 'a DS_Store item encountered'
os.remove(f)

My code walks the folders properly and print prints the statement when a
..DS_Store file is encountered but the os.remove does not get rid of the
file. I have previously cd'd into the folder and can remove all by doing
which does work

system('find . -name .DS_Store -exec rm {} \;')

but I am walking the folders to do a few more things. I can't figure why
remove is not removing.

Regards,
David
 
B

Ben Cartwright

David said:
# Clean mac .DS_Store
if current_file == '.DS_Store':
print 'a DS_Store item encountered'
os.remove(f) ....
I can't figure why
remove is not removing.


It looks like your indentation is off. From what you posted, the
"print" line is prepended with 9 spaces, while the "os.remove" line is
prepended with a single tab. Don't mix tabs and spaces.

Also, shouldn't that be "os.remove(current_file)"?

--Ben
 
D

David Pratt

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)
 
B

Ben Cartwright

David said:
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
 
D

David Pratt

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 said:
David said:
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
 
B

Ben Cartwright

David said:
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.



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
 
D

Dennis Lee Bieber

My code walks the folders properly and print prints the statement when a
.DS_Store file is encountered but the os.remove does not get rid of the
file. I have previously cd'd into the folder and can remove all by doing
which does work
Based upon what Google found, you have to 1) be running as
admin/root to do this, and 2) the Mac "finder" will recreate the files
whenever you visit the directory.

http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_16831
--
 
D

David Pratt

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 said:
David said:
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.





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
 
G

Greg Ewing

David said:
Hi Ben. I hadn't realize that walk was just giving the file name so the
join did the job just great.

I don't think that deleting the .DS_Store files is the
right approach to this, for various reasons:

* You're messing with MacOSX's metadata, which is
not a nice thing to do to it.

* Your .DS_Store may contain info you'd like to keep,
such as icon positions in the window.

* One day you might want to do this under a userid
that doesn't have the necessary permissions.

* It might not even work, since the .DS_Store could
get re-created in between your purge and creating
the tarball.

It would be better to just avoid putting the .DS_Store
files in the tarball. For example, collect the pathnames
of all the .DS_Store files and give them as an exclusion
list to tar.
 
D

Dan Sommers

I don't think that deleting the .DS_Store files is the
right approach to this, for various reasons:

[ ... ]
* It might not even work, since the .DS_Store could
get re-created in between your purge and creating
the tarball.
It would be better to just avoid putting the .DS_Store
files in the tarball. For example, collect the pathnames
of all the .DS_Store files and give them as an exclusion
list to tar.

If these are both true, then *new* .DS_Store files could
be created after I've made the list and before I've made
the tarball.

Regards,
Dan
 
E

Eric Deveaud

Dan said:
If these are both true, then *new* .DS_Store files could
be created after I've made the list and before I've made
the tarball.


not python related, but in order to creater the tarball without the .DS_Store
files why don't you use the --exclude=PATTERN option from tar ??

Eric
 
D

Dennis Lee Bieber

If these are both true, then *new* .DS_Store files could
be created after I've made the list and before I've made
the tarball.
From what I read, it /should/ be safe -- as long as you don't use
the Mac finder on any directories during the list creation AND tar
operation. My understanding was that the files were created/modified
when you visit a directory with the finder.
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top