1.Python3关于文件的操作
1.写了一个简单的Demo,就是向txt文本写入内容,最初代码如下:
file = open("D:/Users/nancy/python.txt","wb") file.write('Hello python file') file.close()
运行结果如下图
2.后面百度了下,发现要用转码方式解决,具体代码如下:
file = open("D:/Users/nancy/python.txt","wb") file.write('Hello python file'.encode()) file.close()
或者
file = open("D:/Users/nancy/python.txt","wb") file.write(bytes("Hello python file","UTF-8")) file.close()