473,388 Members | 1,327 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Removing .DS_Store files from mac folders

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
9 10487
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Karuppasamy | last post by:
Hi I want to populate all the files and folders of System in a Treeview control like Windows Explorer. I try this using File System Objects. But sometimes I am getting an error like 'Access...
2
by: Glen | last post by:
As I understand it, when the first page of an application is accessed, all ASPX/ASCX/etc. files in the same folder are compiled using the JIT compiler. Is there a way to turn this feature off? ...
4
by: Ram | last post by:
Hey, I'v managed to find a way of adding NTFS permissions to a certain folder, But the problem is, the folder has a couple of inherited permissions which I want to delete. How can I remove the...
1
by: Military Smurf | last post by:
Okay, so I have some code that can do a recursive search to get the raw number of files and folders. No big. As a user, I can right click on any parent folder, get the properites, and then on...
0
MMcCarthy
by: MMcCarthy | last post by:
This is a module that scans for files and folders on a specified path and describe them in comma separated values file in a text format. The information is stored in this file consecutively like:...
1
by: semedao | last post by:
Hi all, I'm looking for some example , open source etc code that implement caching of files and folders with those requirments: 1. like every file system , I can write , read , delete etc files...
0
by: =?ISO-8859-1?Q?Konrad_M=FChler?= | last post by:
Hi, I try to delete a whole directory-tree using shutil.rmtree(...) But there are always the hidden files and folders (e.g. from the svn ..svn) left. How can I delete -all- files and folders...
5
by: Per Juul Larsen | last post by:
Hi. My application creates empty libraries. How do I ensure that the user copies at least one or more picture files (.jpg) in each of the empty folders ? Result , no empty folders! regards pjl
0
by: samz | last post by:
Hello, Here is a simple PHP recursive file list (with interactive and visual FX) Sam's Files http://acc.jexiste.ch/JPN/RecurciveDIR12.RAR 1. How to ignore/avoid empty folders in this PHP script...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.