I get a File not Found error on a simple Python function open ()

Here is the code that I welded to a simple open (), the Open file input instruction is displayed, but the Close file does not work. This is done in the Idle interface, but not in the command line interface.

Both the program and the file (spelled correctly and all lowercase letters) are on the desktop for this test. Does anyone see what is missing? Open

# Read It
# Demonstrates reading from a text file

input("\n\nPress the enter key to Open file")
print("Opening and closing the file.")
text_file = open("nicole1.txt", "r")
input("\n\nPress the enter key to Close file")
text_file.close()

input("\n\nPress the enter key to exit.")

** Update, Ok, I tried the absolute path, and it was not successful. I have a copy of this file on a flash drive. I ran it in a Windows XP window and in a Windows 7 window, and everything went fine. I take the same flash drive and try to run it in the Windows10 Box, and I had a problem. One comment asked if there is a trace and there is, and this basically indicates that the file does not exist. Now I’m trying to determine if this is a Windows 10 problem. In addition, the code will work in standby mode on both Windows mailboxes (XP and Win10).

+3
source share
3 answers

, , nicole1.txt.txt (- , Windows , Linux ), , Windows 10 Home Pro Ubuntu 16.04. python test.py , test.py , .

Windows, .

, , :

input("\n\nPress the enter key to Open file")
print("Opening and closing the file.")
with open("nicole1.txt", "r") as text_file:
    input("\n\nPress the enter key to Close file")
    text_file.close()
input("\n\nPress the enter key to exit.")

, , , , . , , , .

+1

, , . , "nicole1.txt", , , . , , :

text_file = open(r"C:\Users\YourName\......\nicole1.txt , . , .

, .

( Galax)

0

How do you run this program? If on the command line, put the file in the same folder as the python file containing this code. As others have pointed out, the problem is that the program cannot find the file. If you run this through some IDE, set the working directory to this path. For example, Pycharm (and other Intellij IDEs) have this in “Configuration Editing”.

-1
source

Source: https://habr.com/ru/post/1617160/


All Articles