Basis of File Input and Output
In the examples that have been
developed so far, all of the data are lost when the program is finished, and
when a new program is started, it is then necessary to enter the data again.
Therefore, Python also has the ability to save and use data easily by accessing
files.
The basic syntax for file input and
output is as follows.
File object = open(file name, open mode) #(1) File object.close() #(2) Open mode r read: Open for read w write: Open for write a append: Open for append |
(1) Creating Object:
Open the file object to handle files with a specified name. Depending on the
open mode, it is possible to deal with file objects in different ways.
(2) Closing Object:
After the use of the file object has finished, you must close the object. Python
automatically closes all file objects at the end of the program, but if you try
to use the file opened in the “w” mode, an error will occur.
File Handling
The following example can be used
to learn how to create and read a file and add content. If you do not specify
the location at the time of the file creation, the file is created in the same
location as the program. After the “fileFirst.txt” and “fileSecond.txt” files
have been created, let's create a simple program that print out each file.
import os def makeFile(fileName, message, mode): #(1) a=open(fileName, mode) #(2) a.write(message) #(3) a.close() #(4) def openFile(fileName): #(5) b=open(fileName,
"r") #(6) lines = b.readlines() #(7) for line in lines: #(8) print(line) b.close() makeFile("fileFirst.txt","This is my first
file1\n","w") #(9) makeFile("fileFirst.txt","This is my first
file2\n","w") makeFile("fileFirst.txt","This is my first
file3\n","w") makeFile("fileSecond.txt","This is my second file
1\n","a") #(10) makeFile("fileSecond.txt","This is my second file
2\n","a") makeFile("fileSecond.txt","This is my second file
3\n","a") print("write fileFirst.txt") print("-----------------------------") openFile("fileFirst.txt") #(11) print("-----------------------------") print("\n") print("write secondFirst.txt") print("-----------------------------") openFile("fileSecond.txt") #(12) print("-----------------------------") >>> write fileFirst.txt ----------------------------- This is my first file3 ----------------------------- write secondFirst.txt ----------------------------- This is my second file 1 This is my second file 2 This is my second file 3 ----------------------------- |
File
Handling
(1) Creating
a Function: To handle a file, a function is declared to receive the file
name, message, an open mode as an argument.
(2)
Opening File: Creates a file object with the specified file name and open
mode.
(3)
Writing File: Records the message
received in the file depending on the mode.
(4) Closing
Object: After the use of the file object is
finished, the object is closed. To create a more efficient program, it
is preferable to place “open()” before and “close()” after the user-defined
function. To provide for a simple explanation, place it inside the user-defined
function.
(5)
Creating a Function: Declare a
function that receives the file name as an argument.
(6) Opening
File: Create a file object that opens
the file in the “r” mode.
(7) Reading
the Content: Read all of the content contained in the file and save it to
the list variable "lines".
(8)
Loop: Repeat as many times as the
number stored in the list.
(9) Creating
a Write Mode File: Create a file named "fileFirst.txt" in the
write mode. While this is repeated three times to record the content, in the
write mode, only one piece of content that is recorded at last remains.
(10) Creating
an Append Mode File: Create a file named "fileSecond.txt" in the
append mode. All content that was repeatedly recorded three times is stored in
the file.
(11) Opening
the File: Open the file named “fileFirst.txt” for which you want to print
the content. Only one row is printed.
(12) Opening
the file: Open the file named “fileSecond.txt” for which you want to print
the content. All three lines are printed.
You can copy and delete the files using
a variety of modules, and it is is possible to move and copy by using the
“shutil” module, and to delete the file by using the “os” module.