Home > Article > Backend Development > Python learning - input and output
Input and output
File
You can open a file by creating an object of the file class, and use the read, readline or write methods of the file class to read and write the file appropriately. The ability to read and write files depends on the mode you specify when opening the file. Finally, when you are done with the file, you call the close method to tell Python that we are done with the file.下 Examples of using files are as follows:
poem = '''''\Programming is fun When the work is done if you wanna make your work also fun:use Python!''' f = file('poem.txt', 'w') f.write(poem) f.close() f = file('poem.txt') while True: line = f.readline() if len(line) == 0: break print line, f.close()Output: Python code
Programming is fun When the work is done if you wanna make your work also fun: use Python!First, you can create an instance of the File class by indicating the files and modes we want to open. Mode can be read mode ('r'), write mode ('w'), or append mode ('a'). There are many other modes available, use help(file) to learn more about them. We open the file in write mode, then use the write method of the file class to write the file, and finally we use close to close the file.
Next, we open the same file again to read the file. If we do not specify a mode, read mode will be used as the default mode. In a loop, we use the readline method to read each line of the file. This method returns a complete line including the newline character at the end of the line. So, when an empty string is returned, it means that the end of the file has been reached, so we stop the loop.
Note that because the content read from the file already ends with a newline character, we use commas on the print statement to eliminate automatic
newlines.
Finally, we close the file with close.
There is another module called cPickle, which does exactly the same thing as the pickle module, except that it is written in C, so it is much faster (1000 times faster than pickle). You can use any of them, but we will use the cPickle module here. Remember, we refer to both modules as pickle modules for short.存 Storage and storage examples are as follows:
Python code
import cPickle as p shoplistfile = 'shoplist.data' shoplist = ['apple', 'mango', 'carrot'] f = file(shoplistfile, 'w') p.dump(shoplist, f) f.close() del shoplist f = file(shoplistfile) storedlist = p.load(f) print storedlistE
Output:
Python code
['apple', 'mango', 'carrot']
First, please note that we use Import many grammar. This is a convenience method so that we can use shorter module names. In this example, it also allows us to switch to another module (cPickle or pickle) by simply changing one line! Throughout the rest of the program, we simply call this module p.
In order to store an object in a file, first open a file object in write mode, and then call the dump function of the storage module to store the object in the open file. This process is called storage. Next, we use the return of the load function of the pickle module to retrieve the object. This process is called storage retrieval.