29
loading...
This website collects cookies to deliver better user experience
How will the interpreter know when to end a line? Each line in a file has the EOL terminating character (example comma or newline character) which the interpreter reads and processes a new line..
file = open('myfile.txt', 'r') # Reading mode
file = open('myfile.txt', 'a') # Writing mode
file = open('myfile.txt', 'w') # Appending mode
file = open('myfile.txt', 'r+') # Both reading and writing
Note than the file name is case sensitive. So myfile.txt
is not equal to Myfile.txt
myfile.txt
A Quick brown fox jumps over the lazy dog
Welcome to PYTHON Programming
Traceback (most recent call last):
File "main.py", line 1, in <module>
file = open("myfile.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'myfile.txt'
file.read()
methodfile = open("myfile.txt", "r")
print (file.read())
A Quick brown fox jumps over the lazy dog
Welcome to PYTHON Programming
file = open("myfile.txt", "r")
print (file.read(7))
A Quick
file = open("myfile.txt", "r")
print (type(file.read(7)))
<class 'str'>
file = open("myfile.txt", "r")
for temp in file:
print (temp)
A Quick brown fox jumps over the lazy dog
Welcome to PYTHON Programming
file = open('myfile.txt','w')
file.write("A Quick brown fox jumps over the lazy dog.")
file.write("Welcome to PYTHON Programming")
file.close()
A Quick brown fox jumps over the lazy dog.Welcome to PYTHON Programming
The close() command terminates all the resources in use and frees the system of this particular program.
\n
symbol.file = open('myfile.txt','w')
file.write("A Quick brown fox jumps over the lazy dog.")
file.write("\n")
file.write("Welcome to PYTHON Programming")
file.close()
A Quick brown fox jumps over the lazy dog.
Welcome to PYTHON Programming
file = open('myfile.txt','a')
file.write("A Quick brown fox jumps over the lazy dog.")
file.write("\n")
file.write("Welcome to PYTHON Programming")
file.close()
A Quick brown fox jumps over the lazy dog.
Welcome to PYTHON ProgrammingA Quick brown fox jumps over the lazy dog.
Welcome to PYTHON Programming