Before you read or write a file using the python built-in read/write function, you need to "open" the file first.
語法為 fo = open('filename', 'open_mode_option')<br> (fo可以是自己決定的變數名稱,指向被開啟的檔案,稱為fileObject,後續讀取檔案的時候會使用到)
針對文字檔,幾種比較常用到的open mode options:
其他的open mode options可以在這邊找到: https://docs.python.org/2/library/functions.html#open
Once a file is opened and you have one file object, you can get various information related to that file.
Here is a list of all attributes related to file object:
Attribute | Description |
---|---|
file.closed | Returns true if file is closed, false otherwise. |
file.mode | Returns access mode with which file was opened. |
file.name | Returns name of the file. |
# Open a file
fo = open("foo.txt", "wb") # Open for writing in binary format. Overwrites existing file.
print("Name of the file: ", fo.name) # Let's check if the file name is correct
print("Opening mode : ", fo.mode) # Check the read/write/binary mode status
print("Closed or not : ", fo.closed) # Check the open/close status
Name of the file: foo.txt Opening mode : wb Closed or not : False
The close() method of a file object flushes any unwritten information and closes the file object, after which no more writing can be done.
Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.
fileObject.close()
# Open a file
fo = open("foo.txt", "wb")
print("Closed or not : ", fo.closed) # Check the open/close status
# Close opend file
fo.close()
print("Closed or not : ", fo.closed) # Check the open/close status again
Closed or not : False Closed or not : True
The write() method writes any string to an open file. It is important to note that Python strings can have binary data and not just text.<br>
The write() method does not add a newline character ('\n') to the end of the string automatically. In order to output multiple lines, add a '\n' character at the position of the end of each line.
fileObject.write(string)
# Open a file
fo = open("foo.txt", "w") # open for writing only
fo.write("This is the first output.") # output string to the file.
# As no \n is included at the end of the string ,
# the next __write()__ will continue to output to the same line.
fo.write("This is the second output.\n") # Continue the first line.
# A \n is included at the end. The next __write()__ will start a new line
fo.write("Python is a great language.\nYeah its great!!\n") #write two new lines to the file
# Close opend file
fo.close()
fo = open("foo.txt", "r") # open for readingly only; read from the beginning
A=fo.read(7)
print(A)
B=fo.read(10)
print(B)
This is the first
fo = open("foo.txt", "r") # open for reading only; read from the beginning
A=fo.readline() # read only the first line in the file
print(A)
B=fo.readlines() # read the rest of the file (2nd~3rd lines) as a python list
print(len(B)) # checking the size of the list
print(B) # the elements are separated by'\n' in the string
print(B[1]) # the second element in the list
This is the first output.This is the second output. 2 ['Python is a great language.\n', 'Yeah its great!!\n'] Yeah its great!!
fo = open("foo.txt", "r") # open for readingly only; read from the beginning
A=fo.readline() # read only the first line in the file
print(A)
As=A.split(' ')
print(As)
B=fo.readlines()
print(B[1])
Bs=B[1].split(' ')
print(Bs)
print(Bs[0])
This is the first output.This is the second output. ['This', 'is', 'the', 'first', 'output.This', 'is', 'the', 'second', 'output.\n'] Yeah its great!! ['Yeah', 'its', 'great!!\n'] Yeah