Opening a file in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Opening a file in python

f=open (file path with name,"r") cc=f.read() print(cc) file.close() how the path should be mentioned so that I can read the text file created in notepad++

8th Jul 2018, 4:13 PM
Anand Agrawal
5 Answers
+ 2
let suppose our file is in local disk D in folder name code and our file name is first.txt .Then how we specified the path. f=open(r"D:\code\first.txt","r") cc=f.read() print(cc) file.close()
8th Jul 2018, 5:54 PM
Maninder $ingh
Maninder $ingh - avatar
+ 1
yeah if I create a file in same folder then it's ok I can open it with the file name only...but for files created in different folders I am not able to get how to mention right path..
8th Jul 2018, 5:54 PM
Anand Agrawal
+ 1
what is that 'r' just before 'open' u r mentioning in first line
8th Jul 2018, 6:08 PM
Anand Agrawal
+ 1
Manindar Singh: Thankyou without that 'r' error was occuring 😁🙏
8th Jul 2018, 6:10 PM
Anand Agrawal
0
lets say this (this won't work for you, just an example) path: /home/yee/codes/filename.txt
8th Jul 2018, 5:51 PM
Paul Grasser
Paul Grasser - avatar