python可以检测到它在哪个操作系统下运行?

Mer*_*lin 6 python linux windows

python可以检测操作系统,然后为文件系统构造一个if/else语句.

我需要用FileSys字符串替换Fn字符串中的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)
Run Code Online (Sandbox Code Playgroud)

pok*_*oke 16

我通常只使用这个:

import os
if os.name == 'nt':
    pass # Windows
else:
    pass # other (unix)
Run Code Online (Sandbox Code Playgroud)

编辑:

希望回应您的意见:

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' ) )
Run Code Online (Sandbox Code Playgroud)

  • 咦?你那是什么意思? (2认同)

小智 6

请参阅此处:https ://stackoverflow.com/a/58689984/3752715

import platform 
plt = platform.system()

if   plt == "Windows":   print("Your system is Windows")
elif plt == "Linux":     print("Your system is Linux")
elif plt == "Darwin":    print("Your system is MacOS")
else:                    print("Unidentified system")
Run Code Online (Sandbox Code Playgroud)

你可以看到我的github仓库https://github.com/sk3pp3r/PyOS并使用pyos.py脚本


Ela*_*fer 5

使用sys.platform. 您可以在此处找到更多信息http://docs.python.org/library/platform.html