Python 错误记录(1)Fatal Python error- Py_Initialize- can't initialize sys standard streams AttributeErro

Python 错误记录(1)Fatal Python error- Py_Initialize- can't initialize sys standard streams AttributeErroFatal Python error Py Initialize can t initialize sys standard streams AttributeErr python3 5 pycharm 不确定其他环境会不会报错 from datetime import datetime with open C

大家好,我是讯享网,很高兴认识大家。

Fatal Python error: Py_Initialize: can’t initialize sys standard streams AttributeErro

python3.5+pycharm ,不确定其他环境会不会报错。

from datetime import datetime with open('C:\Users\hhh\Desktop\sample.txt', 'w') as f: f.write('今天是 ') f.write(datetime.now().strftime('%Y-%m-%d')) with open('C:\Users\hhh\Desktop\sample.txt', 'r') as f: s = f.read() print('open for read...') print(s) with open('C:\Users\hhh\Desktop\sample.txt', 'rb') as f: s = f.read() print('open as binary for read...') print(s) 

讯享网

出现如下错误


Fatal Python error: Py_Initialize: can’t initialize sys standard streams
AttributeError: module ‘io’ has no attribute ‘OpenWrapper’

Current thread 0x00003a68 (most recent call first):

排查过程 error1

这里写图片描述
讯享网
发现包名io有问题,在这个报名下即使是一条简单的print(’hello’)语句都会报上述错误,于是将包名改hhh(随便一个包名)。

error2

将报名改后,又出现了如下报错
with open(‘C:\Users\hhh\Desktop\sample.txt’, ‘w’) as f:
^
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
经过调试发现文件路径格式不对

讯享网C:\Users\hhh\Desktop\sample.txt

将上述路径改为

C:\\Users\hhh\Desktop\sample.txt
讯享网from datetime import datetime with open('C:\\Users\zhaorongrong\Desktop\sample.txt', 'w') as f: f.write('今天是 ') f.write(datetime.now().strftime('%Y-%m-%d')) with open('C:\\Users\zhaorongrong\Desktop\sample.txt', 'r') as f: s = f.read() print('open for read...') print(s) with open('C:\\Users\zhaorongrong\Desktop\sample.txt', 'rb') as f: s = f.read() print('open as binary for read...') print(s) 

运行结果
open for read…
今天是 2018-04-04
open as binary for read…
b’\xbd\xf1\xcc\xec\xca\xc7 2018-04-04’

Process finished with exit code 0

使用Python的r前缀,就不用考虑转义的问题了

from datetime import datetime with open(r'C:\Users\zhaorongrong\Desktop\sample.txt', 'w') as f: f.write('今天是 ') f.write(datetime.now().strftime('%Y-%m-%d')) with open(r'C:\Users\zhaorongrong\Desktop\sample.txt', 'r') as f: s = f.read() print('open for read...') print(s) with open(r'C:\Users\zhaorongrong\Desktop\sample.txt', 'rb') as f: s = f.read() print('open as binary for read...') print(s)
讯享网'C:\Users\hhh\Desktop\sample.txt'

将上述路径改为

r'C:\Users\hhh\Desktop\sample.txt'
小讯
上一篇 2025-02-05 18:12
下一篇 2025-01-14 22:27

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/55889.html