By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

Removing .DS_Store files from mac folders

P: n/a
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
Mar 2 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
David Pratt wrote:
# 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

Mar 2 '06 #2

P: n/a
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)

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

Mar 2 '06 #3

P: n/a
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

Mar 2 '06 #4

P: n/a
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

Mar 2 '06 #5

P: n/a
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

Mar 2 '06 #6

P: n/a
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

Mar 2 '06 #7

P: n/a
David Pratt wrote:
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.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Mar 9 '06 #8

P: n/a
On Thu, 09 Mar 2006 17:19:15 +1300,
Greg Ewing <gr**@cosc.canterbury.ac.nz> wrote:
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

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
Mar 9 '06 #9

P: n/a
Dan Sommers wrote:
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

--
CM: Faut-il nommer un modérateur suppléant à Christophe Wolfhugel ?
MG: Faudrait savoir ce qu'il en pense ? Va-t-il répondre : « Rien » ?
CW: Non.
-+- in: Guide du Cabaliste Usenet - Euh... Non, rien. -+-
Mar 9 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.