thegrinner
thegrinner

Reputation: 12241

Is there an equivalent to Python's os.remove() on Mac?

Currently I have some code deleting some temporary files created by my program:

# Delete the generated files
exts = [".lsys", ".py", ".pyc"]
for ext in exts:
    os.remove("{0}{1}{2}".format(self.grammarDir, filename, ext))

Now I'm trying to port the application to Mac. Looking at the documentation for Python 2.7, it specifically says:

Remove (delete) the file path. If path is a directory, OSError is raised; see rmdir() below to remove a directory. This is identical to the unlink() function documented below. On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.

Availability: Unix, Windows.

Is there an equivalent to os.remove() for Mac or am I stuck using something like this?

os.system("rm {0}{1}{2}".format(self.grammarDir, filename, ext))

I need compatibility with Mac, and maintaining compatibility with Ubuntu would be a huge bonus (but isn't strictly necessary).

Edit:

Well, now I feel foolish. Turns out I had a broken call above this segment of code so the deletion code wasn't being reached. Misdiagnosed where my error was, thought it was failing silently.

Upvotes: 2

Views: 2784

Answers (3)

phihag
phihag

Reputation: 288180

Mac OS X is a Unix, too. From the top of the linked documentation:

If not separately noted, all functions that claim “Availability: Unix” are supported on Mac OS X, which builds on a Unix core.

Upvotes: 7

David Heffernan
David Heffernan

Reputation: 613352

os.remove is available on Windows and Unix. Max OSX counts as Unix so you can use os.remove on Mac OSX.

From the documentation that you linked to:

If not separately noted, all functions that claim “Availability: Unix” are supported on Mac OS X, which builds on a Unix core.

Upvotes: 4

Jacob
Jacob

Reputation: 43299

Unix in this context means Mac and Linux, too. Problably all Unix-like/POSIX-compliant systems that you can build Python on.

Upvotes: 5

Related Questions