Please enable JavaScript.
Coggle requires JavaScript to display documents.
File Handling (summary (A file is an object to store data, Files are…
File Handling
summary
-
Files are stored in many different types of storage media, including hard disk and dvd
A file can be opened in a number of different modes - read-only, write and append
-
If the data is formatted, then the code can make use of this to read in the data correctly
-
It depends on the file open mode as to whether the original data it contained is discarded or added to at the end of the file
-
Read
-
filehandle = open('mytext.txt', 'r')
-
-
fh = open('mytext.txt', 'r')
-
Open
Myfile = open ('filename', 'mode')(mode=txt etc)
-
-
-
Write
filehandle = open('mytextfile.txt', 'w')
-
The first line of code shows a file called mytextfile.txt being opened in 'write' mode. If the file doesn't exist, the open command will create it. If mytextfile.txt already exists, then all the data within it is discarded, ready for the program to write new data into it.
If the data needs to be kept, then don't use 'write'. Instead, open the file in 'append' mode. This will keep the existing data intact, and any new data is added to the end of the file.
Close
-
filehandle = open('mytextfile.txt', 'r')
-