Python 错误记录(1)Fatal Python error: Py_Initialize: can't initialize sys standard streams AttributeErro
Fatal Python error: Py_Initialize: can’t initialize sys standard streams AttributeErro python3.5+pycharm ,不确定其他环境会不会报错。
from datetime import datetimewith 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
正确代码如下
为什么需要改成这样呢?是因为Python的字符串本身也用\转义,所以要特别注意,建议使用Python的r前缀,就不用考虑转义的问题了
from datetime import datetimewith 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 datetimewith 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'
【Python 错误记录(1)Fatal Python error: Py_Initialize: can't initialize sys standard streams AttributeErro】将上述路径改为
r'C:\Users\hhh\Desktop\sample.txt'
推荐阅读
- 推荐系统论文进阶|CTR预估 论文精读(十一)--Deep Interest Evolution Network(DIEN)
- Python专栏|数据分析的常规流程
- Python|Win10下 Python开发环境搭建(PyCharm + Anaconda) && 环境变量配置 && 常用工具安装配置
- Python绘制小红花
- Pytorch学习|sklearn-SVM 模型保存、交叉验证与网格搜索
- OpenCV|OpenCV-Python实战(18)——深度学习简介与入门示例
- python|8. 文件系统——文件的删除、移动、复制过程以及链接文件
- 爬虫|若想拿下爬虫大单,怎能不会逆向爬虫,价值过万的逆向爬虫教程限时分享
- 分布式|《Python3网络爬虫开发实战(第二版)》内容介绍
- java|微软认真聆听了开源 .NET 开发社区的炮轰( 通过CLI 支持 Hot Reload 功能)