python可以检测操作系统,然后为文件系统构造一个if / else语句。
我需要用FileSysstringreplaceFnstring中的C:\ CobaltRCX \。
import os.path, csv from time import strftime if os.path.?????:## Windows FileSys = r"C:\\working\\" else: ##linux FileSys = r"\\working\\" y=(strftime("%y%m%d")) Fn = (r"C:\\working\\Setup%s.csv" %y)
我通常只是使用这个:
import os if os.name == 'nt': pass # Windows else: pass # other (unix)
希望能回应你的评论:
from time import strftime import os if os.name == 'nt': # Windows basePath = 'C:\\working\\' else: basePath = '/working/' Fn = '%sSetup%s.csv' % ( basePath, strftime( '%y%m%d' ) )
使用sys.platform
。 你可以在这里找到更多的信息http://docs.python.org/library/platform.html
对于大多数的应用程序,你应该使用os.platform
模块。 但是,如果您需要更精益的界面,请尝试platinfo
。
试试这个:
import platform platform.uname()
它既可以在linux上运行,也可以在windows上运行。 仅供参考:os.uname()在Windows上不起作用,虽然它在linux上工作。 平台是通用的。
你可以看看os.uname
In [12]: os.uname() Out[12]: ('Darwin', 'demitasse.local', '10.6.0', 'Darwin coreel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386', 'i386')
是。
>>> import os >>> os.uname() ('Linux', 'ubuntu', '2.6.32-27-generic', '#49-Ubuntu SMP Thu Dec 2 00:51:09 UTC 2010', 'x86_64') >>> system = os.uname() >>> print system[0] + '/' + system[1] Linux/ubuntu >>>