+ 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.
3 Antworten
+ 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..
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)
0
First Set the directory of the file...
And then execute with
Open("file.txt",r+)
#to both read and write