1

1.I am new to coding and I store all my python code in:

C:\Users\(MyName)\Documents\Python Scripts

2.I made a txt file in notepad (list.txt) and stored it in the same location

3.I want to read the file so in Spyder:

file = open('list.txt', 'r')
print(file.read())
file.close()

4.I get an error

FileNotFoundError: [Errno 2] No such file or directory: 'list.txt'

5.Where am I supposed to store my txt file? And how do I read it?

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
emily
  • 11
  • 1
  • 2
  • Are you sure the script and file are in the same location, and you're running them from that location? Make sure your working directory in Spyder is `C:\Users\(MyName)\Documents\Python Scripts` – Pranav Hosangadi Feb 01 '21 at 19:19
  • now it says runfile('C:/Users/(MyName)/Documents/Python Scripts/(FileName.py)', wdir='C:/Users/(MyName)/Documents/Python Scripts') – emily Feb 01 '21 at 20:48

2 Answers2

0

Make sure you have the script in the same directory as the file, or change the path to the correct directory path.

llamaking136
  • 421
  • 5
  • 16
-1

A possible solution is using numpy.loadtxt and 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