![]() |
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")
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? 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()
In [4]: with open("CSAT.txt", "r") as File2: file_stuff=File2.read() print(file_stuff) print(Fi1e2.closed) print(file_stuff)
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. |