Python Forum
Python help w/ .txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python help w/ .txt file
#1

  1. Hi so my assignment asks that I use text saved under 'numbers.txt'
  2. I am using Windows, so I used notepad to write the text and save it under the name
  3. However, when I go to run the code, I get the error Errno 2 no such file or directory.
Here is the code I have.

def main():
    infile = open('numbers.txt', 'r')

    file_contents = infile.read()
    infile.close()

    print(file_contents)

main()
I hope I used the tags correctly!!! I am new!
Reply
#2
You need to run the program from the same folder that numbers.txt is in.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I see, I had it saved on the desktop instead of the same folder. That worked. Seriously, thank you for all your help,
ichabod801! :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020