Liam
Liam

Reputation: 125

How do i solve "FileNotFoundError: [Errno 2] No such file or directory:" in Python whilst using Visual Studio Code?

I have just started to learn Python and I watched this video tutorial - https://www.youtube.com/watch?v=rioXu6EBN0s

when I attempted to create my own .hmtl file using the method in the video I got this error -

FileNotFoundError: [Errno 2] No such file or directory:

this is what I have entered into Visual Studio-

my_variable = "<html><body><p>This is a paragraph.</p><p>This is another paragraph.</p></body></html>"

my_html_file = open("C:\Users\Liam\Documents\Coding\My Files\Python\My Tests/my_html_file.html" "w")

my_html_file.write(my_variable)

I have tried changing to

/Users/Liam.Documents/Coding/My Files/Python/My Tests/
/my_html_file.html

but it hasn't worked, any suggestions would be greatly appreciated.

liam

Upvotes: 1

Views: 2526

Answers (2)

Geetansh G
Geetansh G

Reputation: 17

The reason for this is that you are missing a comma between the filename and the mode that you are using to open the file. Due to this, the interpreter is trying to read the file, which will lead to an error if the file you are requesting does not exist.

Upvotes: 1

misterlinkloquendo19
misterlinkloquendo19

Reputation: 61

Whenever you need to debug, you need to change the path of the file to open it in debug mode by simply:

  1. Going to the explorer
  2. Right click on the file and click on copy relative path
  3. Put that path in open

Also I saw that you put this in your code:

my_html_file = open("C:\Users\Liam\Documents\Coding\My Files\Python\My Tests/my_html_file.html" "w")

You're missing a coma before the w, it should be like this:

my_html_file = open("C:\Users\Liam\Documents\Coding\My Files\Python\My Tests/my_html_file.html","w")

edit: Lol, now I saw that someone else answer your question by replying, anyway, that should help others too! at least...

Upvotes: 5

Related Questions