Input and output of Python files

Source: Internet
Author: User

1. Documents

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 the 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.

Working with files:

#!/usr/bin/python

# Filename:using_file.py

poem = "' \

Programming is fun

When the work was done

If you wanna make your work also fun:

Use python!

‘‘‘

f = File (' Poem.txt ', ' W ') #open for ' W ' riting

F.write (poem) # Write text to file

F.close () # Close the file


f = File (' Poem.txt ')

# If no mode is specified, ' R ' ead mode was assumed by default

While True:

line = F.readline ()

If Len (line) = = 0: # Zero length indicates EOF

Break

Print line,

# Notice comma to avoid automatic newline added by Python

F.close () # Close the file

[email protected] code]# python using_file.py

Programming is fun

When the work was done

If you wanna make your work also fun:

Use python!

[email protected] code]# cat Poem.txt

Programming is fun

When the work was done

If you wanna make your work also fun:

Use python!

[Email protected] code]#

First, create an instance of the file class by indicating which files and schemas you 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.

Open the file in write mode, then write the file using the Write method of the file class, and finally close the file with close.

Next, 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.

2. Storage device

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 the Cpickle module will be used here. Remember, we call these two modules short as pickle modules.

Storage and Storage:


#!/usr/bin/python

# Filename:pickling.py

Import Cpickle as P

Shoplistfile = ' Shoplist.data '

# The name of the file where we'll store the object

Shoplist = [' Apple ', ' mango ', ' carrot ']

# Write to the file

f = File (Shoplistfile, ' W ')

P.dump (shoplist,f) # Dump the object to a file

F.close ()

Del shoplist # Remove the Shoplist

# read back from the storage

f = File (Shoplistfile)

Storedlist = P.load (f)

Print Storedlist

[email protected] code]# python pickling.py

[' Apple ', ' mango ', ' carrot ']

[email protected] code]# cat Shoplist.data

(LP1

S ' Apple '

P2

As ' Mango '

P3

As ' carrot '

P4

A.

First of all, please note that we have used 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.


Input and output of Python files

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.