Python Forum
Opening a file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Opening a file (/thread-25821.html)



Opening a file - amitsinbox - Apr-12-2020

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



RE: Opening a file - buran - Apr-12-2020

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


RE: Opening a file - amitsinbox - Apr-13-2020

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



RE: Opening a file - buran - Apr-13-2020

(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...


RE: Opening a file - amitsinbox - Apr-13-2020

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'



RE: Opening a file - buran - Apr-13-2020

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)



RE: Opening a file - amitsinbox - Apr-13-2020

Thanks Buran. Really appreciate your taking time and helping others. It worked.