Python file operations

Source: Internet
Author: User

in many cases, you will want your program to interact with the user (possibly yourself). You'll get input from the user and print some results. We can use Raw_input and print statements separately to accomplish these functions. For output, you can also use a variety of str (string) classes. For example, you can use the Rjust method to get a string that is right-aligned at a certain width. Use Help (str) for more details. Another common input/the output type is the processing file. The ability to create, read, and write files is necessary for many programs, and we'll explore how to implement them in this chapter. Files you can open a file by creating an object of the file class, using the read, ReadLine, or write method of the file class to read and write files appropriately. The ability to read and write to a file depends on the pattern you specify when you open the file. Finally, when you are done with the file, you call the Close method to tell Python that we have finished using the file. Use file F= File ('E:/poem.txt','W') f.write (poem)#write text to fileF.close () F= File ('E:/poem.txt') whileTrue:line=F.readline ()ifLen (line) = =0: Break    Printline,f.close () output programming isFunwhen the work is Doneifyou wanna made your work also fun:use python! how it works first, we create an instance of the file class by indicating which files and schemas we want to open. The mode can be read mode ('R'), write mode ('W') or Append mode ('a'). In fact there are many more patterns to use, and you can use Help to learn more about them. We first open the file in write mode, then write the file using the Write method of the file class, and finally we close the file with close. Next, we open the same file again to read the file. If we do not specify a pattern, the read mode will be the default mode. In a loop, we use the ReadLine method to read each line of the file. This method returns a complete line that includes the line end newline character. So, when an empty string is returned, it means that the end of the file has arrived, 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 the wrapping. Finally, we close the file with close. Now, take a look at the contents of the Poem.txt file to verify that the program is working correctly. The storage Python provides a standard module called Pickle. With it you can store any Python object in a file, and then you can take it out intact. This is referred to as a persistent storage object. There is another module called Cpickle, which functions exactly the same as the Pickle module, except that it is written in C and therefore much faster (1000 times times faster than pickle). You can use either of them, and we'll use the Cpickle module here. Remember, we call these two modules short as pickle modules. Store and Fetch storageImportCpickle as Pshoplistfile='Shoplist.data'shoplist= ['Apple','Mango','Carrot']f= File (Shoplistfile,'W') P.dump (Shoplist, F)#dump the object to a filef.close ()delshoplist F=file (shoplistfile) storedlist=p.load (f)Printstoredlist Output ['Apple','Mango','Carrot'] How does it work first, please note that we used the import: As syntax. This is a convenient way to make it easier for us to use shorter module names. In this case, it also allows us to switch to another module (Cpickle or pickle) by simply changing one line! In the rest of the program, we simply refer to this module as P. 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 an 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 fetching storage. 

Python file operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.