Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

macos

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.

like image 890
thegrinner Avatar asked Jan 25 '26 07:01

thegrinner


1 Answers

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.

like image 141
phihag Avatar answered Jan 26 '26 21:01

phihag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!