-1

I created a text file with name important on windows and stored the file in documents but when I try to access the file with the command open('important') I get the following traceback message:

C:\Users\donad trump\Documents\python4me\train>python.exe test4.py 
Traceback (most recent call last): 
File "test4.py", line 1, in <module> file = open('important') 
FileNotFoundError: [Errno 2] No such file or directory: 'important' 

My code is:

file = input('important')
reat = file.read()
print(reat)
Pavel Štěrba
  • 2,822
  • 2
  • 28
  • 50
donald
  • 1
  • 1
  • 2
    give the full path then – Pygirl Apr 07 '20 at 19:38
  • 1
    Your Windows may be set to automatically hide known file extensions. If you used something like Notepad to create your text file, its name is not what you think. – Jongware Apr 07 '20 at 19:44
  • C:\Users\donad trump\Documents\python4me\train>python.exe test4.py File "test4.py", line 1 file = input('C:\Users\donad trump\Documents\important.txt') ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape C:\Users\donad trump\Documents\python4me\train> i still get this error message, thanks !! i still need your help – donald Apr 07 '20 at 19:49
  • https://stackoverflow.com/questions/1347791/unicode-error-unicodeescape-codec-cant-decode-bytes-cannot-open-text-file – Pavel Štěrba Apr 07 '20 at 19:53

2 Answers2

2

Honest opinion, from your code, my guess is that you are a beginner with Python (totally fine!) and you have not checked any kind of available help. It is ok to be a beginner, everyone was. But you should take the time to read how to open files in Python in the many (many, many, many...) available sources.

This is a good one:

https://www.w3schools.com/python/python_file_open.asp

But surely, you can find many others by just googling "read file Python 3".

non87
  • 115
  • 1
  • 8
0

Here:

with open("path/to/important.txt", "r") as file:
     trump_should_wear_a_mask = file.read()
bug_spray
  • 1,445
  • 1
  • 9
  • 23