Some times you may want to interact with an outside file. Maybe you want you program to create a text file to store data or open one to read data. Python allows for this as we will see below.
First create a file named “test.txt” and save it in the same directory as your Python files.
#opening an existing file f = open('test.txt', 'r') print() print("1.") print(f.read()) #now the invisible cursor is at the end of the file print()
What invisible cursor you ask? Well when the file is open the cursor is located at the very beginning. As they file is read the cursor moves forward. If the entire file is read the cursor will be at the end. We will keep adding to this file.
#opening an existing file f = open('test.txt', 'r') print() print("1.") print(f.read()) #now the invisible cursor is at the end of the file print() f.seek(0,0) #moves the cursor back to the beginning of the file print("2.") x = f.read() print(x) #prints the entire file print()
The f.seek relocates the cursor to the beginning of the files. (0,0 = 0 position, beginning).
#opening an existing file f = open('test.txt', 'r') print() print("1.") print(f.read()) #now the invisible cursor is at the end of the file print() f.seek(0,0) #moves the cursor back to the beginning of the file print("2.") x = f.read() print(x) #prints the entire file print() f.seek(7,0) #puts the cursor 7 bytes from the beginning removing first name print("3.") x = f.read() print(x) #prints everything after the 7 bytes print()
The new codes skips over the first 7 characters, then prints the test.
#opening an existing file f = open('test.txt', 'r') print() print("1.") print(f.read()) #now the invisible cursor is at the end of the file print() f.seek(0,0) #moves the cursor back to the beginning of the file print("2.") x = f.read() print(x) #prints the entire file print() f.seek(7,0) #puts the cursor 7 bytes from the beginning removing first name print("3.") x = f.read() print(x) #prints everything after the 7 bytes print() f.seek(17,0) #puts the cursor at the beginning +17 print("4.") y = f.readline() print(y)
The new code skips the first 17 characters.
#opening an existing file f = open('test.txt', 'r') print() print("1.") print(f.read()) #now the invisible cursor is at the end of the file print() f.seek(0,0) #moves the cursor back to the beginning of the file print("2.") x = f.read() print(x) #prints the entire file print() f.seek(7,0) #puts the cursor 7 bytes from the beginning removing first name print("3.") x = f.read() print(x) #prints everything after the 7 bytes print() f.seek(17,0) #puts the cursor at the beginning +17 print("4.") y = f.readline() print(y) f.seek(0,0) #puts the cursor at the beginning print("5.") z = f.readlines() #returns a list of the lines [line1, line2, etc...] print(z) print()
The new code reads the lines and adds them to a list.
#opening an existing file f = open('test.txt', 'r') print() print("1.") print(f.read()) #now the invisible cursor is at the end of the file print() f.seek(0,0) #moves the cursor back to the beginning of the file print("2.") x = f.read() print(x) #prints the entire file print() f.seek(7,0) #puts the cursor 7 bytes from the beginning removing first name print("3.") x = f.read() print(x) #prints everything after the 7 bytes print() f.seek(17,0) #puts the cursor at the beginning +17 print("4.") y = f.readline() print(y) f.seek(0,0) #puts the cursor at the beginning print("5.") z = f.readlines() #returns a list of the lines [line1, line2, etc...] print(z) print() print("6.") print("Name of the file: ", f.name) f.close() #prints the name of the file print()
You may have guessed the new code prints the file name.
#opening an existing file f = open('test.txt', 'r') print() print("1.") print(f.read()) #now the invisible cursor is at the end of the file print() f.seek(0,0) #moves the cursor back to the beginning of the file print("2.") x = f.read() print(x) #prints the entire file print() f.seek(7,0) #puts the cursor 7 bytes from the beginning removing first name print("3.") x = f.read() print(x) #prints everything after the 7 bytes print() f.seek(17,0) #puts the cursor at the beginning +17 print("4.") y = f.readline() print(y) f.seek(0,0) #puts the cursor at the beginning print("5.") z = f.readlines() #returns a list of the lines [line1, line2, etc...] print(z) print() print("6.") print("Name of the file: ", f.name) f.close() #prints the name of the file print() #read(n) print("7.") y = f.read(7) print() print("The first 7 characters are: ", y) #prints the n letters from the beginning
The new codes reads and prints just the first 7 characters.
#opening an existing file f = open('test.txt', 'r') print() print("1.") print(f.read()) #now the invisible cursor is at the end of the file print() f.seek(0,0) #moves the cursor back to the beginning of the file print("2.") x = f.read() print(x) #prints the entire file print() f.seek(7,0) #puts the cursor 7 bytes from the beginning removing first name print("3.") x = f.read() print(x) #prints everything after the 7 bytes print() f.seek(17,0) #puts the cursor at the beginning +17 print("4.") y = f.readline() print(y) f.seek(0,0) #puts the cursor at the beginning print("5.") z = f.readlines() #returns a list of the lines [line1, line2, etc...] print(z) print() print("6.") print("Name of the file: ", f.name) #prints the name of the file print() f.seek(0,0) #puts the cursor at the beginning #read(n) print("7.") y = f.read(7) print() print("The first 7 characters are: ", y) #prints the n letters from the beginning #readline print() print("8.") z= f.readline() #prints from the cursor to end of that line only print("This the the rest of the line printed above: ", z) f.close() print()
The new code reads just one line. If the cursor is not at the beginning of that lines, it reads the remaining.
#opening an existing file f = open('test.txt', 'r') print() print("1.") print(f.read()) #now the invisible cursor is at the end of the file print() f.seek(0,0) #moves the cursor back to the beginning of the file print("2.") x = f.read() print(x) #prints the entire file print() f.seek(7,0) #puts the cursor 7 bytes from the beginning removing first name print("3.") x = f.read() print(x) #prints everything after the 7 bytes print() f.seek(17,0) #puts the cursor at the beginning +17 print("4.") y = f.readline() print(y) f.seek(0,0) #puts the cursor at the beginning print("5.") z = f.readlines() #returns a list of the lines [line1, line2, etc...] print(z) print() print("6.") print("Name of the file: ", f.name) #prints the name of the file print() f.seek(0,0) #puts the cursor at the beginning #read(n) print("7.") y = f.read(7) print() print("The first 7 characters are: ", y) #prints the n letters from the beginning #readline print() print("8.") z= f.readline() #prints from the cursor to end of that line only print("This the the rest of the line printed above: ", z) f.close() print() #Using a loop to readline print() print("9.") f = open('test.txt', 'r') for line in f: #line is just a variable, could be "i" or anything print(line, end='') print() f.close()
This new code is an example of using a For loop to read and print a file.
Now what is we want to create a new file and write to that file? We can do that too!
#creates, opens and writes to a file f2 = open("test2.txt", "w") f2.write("This is line one. ") f2.write("See, we are still writing on the first line since no backslash used above.\n") f2.write("Now we are writing on the second line.\n") f2.write("I love Python!\n") f2.close() #appends new data to existing file f2 = open("test2.txt", "a") f2.write("I REALLY love Python!!!") #since it was opened in "a" append mode all writes are appended to the end #with - read statement loop print("1st With Example") with open("test2.txt") as file: # Use file to refer to the file object data = file.read() print(file) print() #another with - read statements loop print("2nd With Example") with open("test2.txt") as f: for line in f: print(line)
This time I just gave it to you all at once. The first section creates and writes to a new file. The second sections appends new data to the end of that same file. The third and fourth sections are loops to read the new file.
Often if you are having trouble with reading a writing, it is because of leaving the file open. If you open a file for reading you cannot write to it until it is closed and vice versa.
Great work!