Python Forum
Phyton Opening files on windows C: pc
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Phyton Opening files on windows C: pc
#1
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.
Reply
#2
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'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Photo 
(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.    
Reply
#4
the syntax error is because you miss closing quote for the filename string
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rename files in a folder named using windows explorer hitoxman 3 759 Aug-02-2023, 04:08 PM
Last Post: deanhystad
Exclamation Phyton Metin2 Game Left Click not working. Help anonym35 0 1,547 Feb-02-2022, 07:01 PM
Last Post: anonym35
  Copying files from a remote Windows station tester_V 11 6,907 Jul-17-2021, 02:19 AM
Last Post: tester_V
  opening files and output of parsing leodavinci1990 4 2,556 Oct-12-2020, 06:52 AM
Last Post: bowlofred
  Opening and closing Mac applications and files Nickd12 5 5,737 Sep-05-2020, 04:39 AM
Last Post: perfringo
  PYHTON not opening files profficial 11 13,620 Feb-15-2020, 09:06 AM
Last Post: bharathendra
  Phyton code to load a comma separated csv file in to a dict and then in to a dB mrsenorchuck 2 2,682 Nov-29-2019, 10:59 AM
Last Post: mrsenorchuck
  Code works in IDLE, appears to work in CMD, but won't produce files in CMD/Windows ChrisPy33 3 3,249 Jun-12-2019, 05:56 AM
Last Post: ChrisPy33
  opening a file from a windows desktop folder- error opening file Charan007 1 2,990 Dec-06-2018, 11:50 AM
Last Post: buran
  Python files made on linux don't work on windows sylas 2 3,267 Oct-10-2018, 05:34 AM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020