为什么 Python2 和 Python3 对同一个 windows 目录的处理方式不同?

编程入门 行业动态 更新时间:2024-10-10 12:26:21
本文介绍了为什么 Python2 和 Python3 对同一个 windows 目录的处理方式不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的windows语言是中文.为了说明我的观点,我使用包 pathlib.

My windows language is Chinese. To illustrate my point, I use package pathlib.

from pathlib import * rootdir=Path(r'D:\新建文件夹') print(rootdir.exists())

Python2.7 我得到 False

Python3 我得到 True

有什么想法吗?感谢您的建议.

Any ideas?Thanks for any advice.

对于Python2.7,你可以用pip install pathlib"安装pathlib

For Python2.7,you can install pathlib with "pip install pathlib"

推荐答案

在 Python 3 中,字符串默认为 Unicode.在 Python 2 中,它们是在源文件编码中编码的字节字符串.在 Python 2 中使用 Unicode 字符串.

In Python 3 strings are Unicode by default. In Python 2, they are byte strings encoded in the source file encoding. Use a Unicode string in Python 2.

还要确保声明源文件编码并确保源文件以该编码保存.

Also make sure to declare the source file encoding and make sure the source is saved in that encoding.

#coding:utf8 from pathlib import * rootdir=Path(ur'D:\新建文件夹') print(rootdir.exists())

更多推荐

为什么 Python2 和 Python3 对同一个 windows 目录的处理方式不同?

本文发布于:2023-11-16 11:06:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603715.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   目录   windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!