我希望知道在脚本内部是否有一种方法可以知道使用的是哪个版本的Python?以下是我想使用它的当前示例:
如果使用的是Python 2,我想让Python脚本使用Unicode,否则不使用Unicode。我目前安装了Python 2.7.5和Python 3.4.0,并在Python 3.4.0下运行我的当前项目。以下是示例脚本:
_base = os.path.supports_unicode_filenames and unicode or str
出现错误:
_base = os.path.supports_unicode_filenames and unicode or str
NameError: name 'unicode' is not defined
所以为了使它能够正常工作,我将其更改为以下内容:
_base = os.path.supports_unicode_filenames and str
有没有办法将它更改为以下内容:
if python.version == 2:
_base = os.path.supports_unicode_filenames and unicode or str
else:
_base = os.path.supports_unicode_filenames and str