Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Opening a file
#1
when I am trying to open a file in pthton 3 jupytor notebook, I get following error: what wrong am I doing?

file1=open("\C:\Users\Amit Kumar\Desktop\CSAT.txt","w")
Error:
File "<ipython-input-7-6ad445b74704>", line 1 file1=open("\C:\Users\Amit Kumar\Desktop\CSAT.txt","w") ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 3-4: truncated \UXXXXXXXX escape
Reply
#2
Don't use backslash in windows paths. Some combinations are escape sequences like \u

use a raw string, e.g. r"C:\Users\Amit Kumar\Desktop\CSAT.txt"
or forward slash, e.g. "C:/Users/Amit Kumar/Desktop/CSAT.txt"

note that I also removed the backslash at the start
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
It didnt help. I want to use Open Function to open the file. because for further commands it doesnt identify the file.



(Apr-12-2020, 03:32 PM)amitsinbox Wrote: when I am trying to open a file in pthton 3 jupytor notebook, I get following error: what wrong am I doing?

file1=open("\C:\Users\Amit Kumar\Desktop\CSAT.txt","w")
Error:
File "<ipython-input-7-6ad445b74704>", line 1 file1=open("\C:\Users\Amit Kumar\Desktop\CSAT.txt","w") ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 3-4: truncated \UXXXXXXXX escape
Reply
#4
(Apr-13-2020, 08:53 AM)amitsinbox Wrote: It didnt help.
Show your code and how it didn't help. You need to replace your string "\C:\Users\Amit Kumar\Desktop\CSAT.txt" with one of mine...
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
#5
Below is what i get if i am trying to now read the file. Apologies if i am doing any basic mistake as i am noew to this world and also this forum

file2=file1.read()
Error:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-3-31e0a6abdbf3> in <module> ----> 1 file2=file1.read() AttributeError: 'str' object has no attribute 'read'
In [4]:
with open("CSAT.txt", "r") as File2:
    file_stuff=File2.read()
    print(file_stuff)
    print(Fi1e2.closed)
    print(file_stuff)





Error:
--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) <ipython-input-4-08eb243c4725> in <module> ----> 1 with open("CSAT.txt", "r") as File2: 2 file_stuff=File2.read() 3 print(file_stuff) 4 print(Fi1e2.closed) 5 print(file_stuff) FileNotFoundError: [Errno 2] No such file or directory: 'CSAT.txt'
Reply
#6
Your txt file is not in the current working directory (I see it's on the Desktop), so you need to pass full path:
with open(r"C:\Users\Amit Kumar\Desktop\CSAT.txt", "r") as f:
    file_stuff=f.read()
    print(file_stuff)
with open(r"C:\Users\Amit Kumar\Desktop\CSAT.txt", "r") as File2:
    for line in f:
        print(line)
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
#7
Thanks Buran. Really appreciate your taking time and helping others. It worked.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Facing Problem while opening a file through command prompt vlearner 4 1,901 Jan-30-2022, 08:10 AM
Last Post: snippsat
  Subprocesses not opening File Select Dialog teut 2 2,396 Feb-22-2021, 08:07 PM
Last Post: teut
  Opening file and outputting its text content example leodavinci1990 1 1,875 Oct-12-2020, 05:33 AM
Last Post: buran
  File Opening Hitso 10 5,253 Jun-05-2020, 10:35 AM
Last Post: DeaD_EyE
  Test a file for a string without opening it? tester_V 2 2,133 Jun-03-2020, 06:40 PM
Last Post: tester_V
  How to assess elapsed time to execute a .exe file or opening a desktop application? Sudershan 2 2,129 Apr-18-2020, 01:40 PM
Last Post: buran
  Opening CSV file from SFTP server not working cluelessintern 0 2,765 Apr-08-2020, 08:10 PM
Last Post: cluelessintern
  How To Find an Opening and Closing String, Copying Open/Close/Contents to New File davidshq 1 2,026 Mar-03-2020, 04:47 AM
Last Post: davidshq
  Error -3 when opening matlab file in python python_newbie09 0 2,288 Aug-02-2019, 11:51 AM
Last Post: python_newbie09
  python script file not opening in IDLE editor srm 2 4,379 Jun-23-2019, 08:45 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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