windows 中路径使用反斜线,linux下路径使用’/’, windows下的路径在linux下是不能被识别的。
path = os.path.split(os.path.realpath(file))[0] + r’\cmds’, 这样的路径不会被linux所识别
解决方案
- 使用os.path.join
path = os.path.join(os.path.split(os.path.realpath(__file__))[0], 'cmds')
- 使用os.sep, python会根据不同的系统自动选择合适的路径分隔
path = os.path.split(os.path.realpath(__file__))[0] + os.sep + 'cmds'
- 可以将所有的路径都使用正斜线:’/’, 在windows和linux都有效
path = path.replace('\\', '/')
- 使用最新的pathlib模块
import pathlib pathlib.Path('C:\dir', 'cmds') # WindowsPath('C:/dir/cmds') # 或者 pathlib.Path('C:\dir')/'cmds' # WindowsPath('C:/dir/cmds')