Python Forum

Full Version: Phyton Opening files on windows C: pc
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
A newbie here. I am into my lesson opening files on Python. The lesson is to copy a poem on the text editor (note++) . I saved the poem on my desktop for easy locating when required.

Actual Windows path
(C:\User\hp\desktop\Poem.txt)

However following the open() function example to pass the file into a variable as object.
poem=open("/home/pi/Document/Poen.txt")

results in a error no file found.

using the textbook example to open the file as
poem=open("C:/User/hp/desktop/Poem.txt")

When the code was written in the shell, pressing enter returns a file not error:

Python Version 3.11.2

What is the correct way to tell Python where to find the file in this situation. A spoon fed code (written full code) using the windows C: path answer is most welcome.

Thank you for your response in advance.
For start, Windows has Users folder (plural), not User. Also is user really hp?

So, It should be
r'C:\Users\<user name>\Desktop\Poem.txt'
or
'C:/Users/<user name>/Desktop/Poem.txt'
(Mar-29-2023, 12:01 PM)buran Wrote: [ -> ]For start, Windows has Users folder (plural), not User. Also is user really hp?

So, It should be
r'C:\Users\<user name>\Desktop\Poem.txt'
or
'C:/Users/<user name>/Desktop/Poem.txt'

YES!!!,
Thanks a lot Buran my man... The example is using ("....") instead of the ('...")
and here is the screen shot of the code. [attachment=2304]
the syntax error is because you miss closing quote for the filename string