c 做的網(wǎng)站又哪些廈門關(guān)鍵詞優(yōu)化網(wǎng)站
python工具包【1】 – 不同操作系統(tǒng)路徑轉(zhuǎn)換
以下的工具類的作用是根據(jù)不同的操作系統(tǒng),將代碼中的路徑轉(zhuǎn)換成適應(yīng)操作系統(tǒng)的路徑。
代碼
import osclass Base_Tools_Cls:def BasePathConvert_func(self, path):'''根據(jù)不同的操作系統(tǒng),將路徑進(jìn)行轉(zhuǎn)換為不同操作系統(tǒng)的路徑:param path::return:'''# 獲取操作系統(tǒng),Windows是nt; linx 是 posixos_type = os.nameif os_type == 'nt':path = path.replace('/', '\\')elif os_type == 'posix':path = path.replace('\\', '/')else:path = path.replace('\\', '/')return pathBaseTools_Cls = Base_Tools_Cls()
使用方法
if __name__ == '__main__':path = BaseTools_Cls.BasePathConvert_func(r'a\b\c\d')print(path)
輸出結(jié)果
mac端: a/b/c/d
windows端: a\b\c\d