开发者

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 开发者_StackOverflow社区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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜