How can we open any pre existing text file in python using open function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How can we open any pre existing text file in python using open function?

I tried giving path of the text file as an argument in open function but it throws an error.

28th Feb 2019, 12:15 PM
Bhuwan Khanijo
Bhuwan Khanijo - avatar
3 Answers
+ 2
open(r'filename with complete path','r') Using r in front of filename/path, will consider the path in raw text and works.. without this, I also faced errors while opening file with full path..
2nd Mar 2019, 4:30 AM
Aravazhi
0
with open("text_file.txt", "r") as f: print(f.read()) If you wish to write to the file, switch the "r" (read) to "w" (write)
28th Feb 2019, 12:20 PM
Nathan Lewis
Nathan Lewis - avatar
0
First Set the directory of the file... And then execute with Open("file.txt",r+) #to both read and write
1st Mar 2019, 6:25 PM
U.Sravan Kumar
U.Sravan Kumar - avatar