-3

I am running python 3.7 in Wing IDE as an administrator.

The python file is in the same folder (Documents) as the textdoc file.

This is the python code I have:

file = open("textdoc.txt", "r")

print(file.readlines())

file.close()

This is the content of the notepad (.txt) file called textdoc:

blue

red

green

When I run the program, I get this error:

Traceback (most recent call last):
  Python Shell, prompt 2, line 3
builtins.FileNotFoundError: [Errno 2] No such file or directory: 'textdoc.txt'

Another issue is that while I am able to create and write to files in Python using the write mode, I can't actually find the text file that was created anywhere in case I want to edit it manually.

jkdev
  • 9,037
  • 14
  • 52
  • 75
Clock
  • 25
  • 1
  • Does this answer your question? [python: Change the scripts working directory to the script's own directory](https://stackoverflow.com/questions/1432924/python-change-the-scripts-working-directory-to-the-scripts-own-directory) – manveti Dec 22 '19 at 02:58

1 Answers1

0

I think that your two questions are actually the same, that is, your python execute your code at another dir. Try to execute it in the command-line:

cd /path/to/your/program
# Check if your file is there
# For windows:
DIR
# For Linux or MacOS:
ls
# Execute python script:
python pyscript.py
xiaoyu2006
  • 421
  • 4
  • 14