[英]How do I read/write to files in Spyder
1.I am new to coding and I store all my python code in: 1.我是编码新手,我将所有 python 代码存储在:
C:\Users\(MyName)\Documents\Python Scripts
2.I made a txt file in notepad (list.txt) and stored it in the same location 2.我在记事本(list.txt)中制作了一个txt文件并存储在同一个位置
3.I want to read the file so in Spyder: 3.我想在 Spyder 中读取文件:
file = open('list.txt', 'r')
print(file.read())
file.close()
4.I get an error 4.我收到一个错误
FileNotFoundError: [Errno 2] No such file or directory: 'list.txt'
5.Where am I supposed to store my txt file? 5.我应该在哪里存储我的txt文件? And how do I read it?我如何阅读它?
Make sure you have the script in the same directory as the file, or change the path to the correct directory path.确保脚本与文件位于同一目录中,或将路径更改为正确的目录路径。
A possible solution is using numpy.loadtxt
and numpy.savext
.一种可能的解决方案是使用numpy.loadtxt
和numpy.savext
。 Eg:例如:
import numpy as np
example_list = [1,2,3,4]
np.savetxt('example_file.txt',example_list)
data = np.loadtxt ('example_file.txt')
print(data)
>>> [1. 2. 3. 4.]
It should work on the same directory your console or py file are located它应该在您的控制台或 py 文件所在的同一目录上工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.