Program interacts with users
- You'll get input from the user and print some results. We can use separate
raw_input,input
and print
statement to accomplish these functions. Raw_input returns a string, and input returns the literal value, which is equivalent to eval (raw_input ([PROMT]).
- For output, you can also use a variety of
str
(string) classes. For example, you can use rjust
a method to get a string that is right-aligned at a certain width . Use help(str)
for more details.
Another common input/output type is processing files. The ability to create, read, and write files is necessary for many programs, and we'll explore how to implement them in this chapter.
File
You can file
open a file by creating an object of the class, using file
the class read
, readline
or method, write
to read and write the file appropriately. The ability to read and write to a file depends on the pattern you specify when you open the file. Finally, when you're done with the file, you call the close
method to tell Python that we're done with the file.
First, we create an instance of a class by indicating which files and schemas we want to open file
. The mode can be read mode ( ‘r‘
), write mode ( ‘w‘
), or Append mode ( ‘a‘
). In fact there are many more patterns available that you can use help(file)
to learn more about them.
We first open the file in write mode, then use the file
method of the class write
to write the file, and finally we use to close
close the file.
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 readline
the 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 print
use commas on the statement to eliminate the wrapping. Finally, we used to close
close this file.
Now, look at poem.txt
the contents of the file to verify that the program is working correctly.
Storage device
- Python provides a standard module, called the
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
, it functions and pickle
modules exactly the same, but it is written in C language , so much faster ( pickle
1000 times times faster).
You can use either of them, and we'll use the cPickle
module here. Remember, we put these two modules simply as pickle
modules.
The following is primarily a demonstration of usage, where the opening of a file should be written as
Try
With open (Shoplistfile, ' W ') as F:
P.dump (SHOPLIST,F)
Except Ioeerror as Ioerr:
Print "File error in (write to the file):" + str (IOERR)
First, notice 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 (or) by simply changing one line cPickle
pickle
! In the rest of the program, we simply refer to this module p
.
To store an object in a file, first open an object in write mode and file
then call the function of the memory module to dump
store the object in an open file. This process is called storage .
Next, we use the pickle
return of the load
function of the module to retrieve the object. This process is called fetching storage .
---restore content ends---
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 separate
raw_input
and print
statement to accomplish these functions.
- For output, you can also use a variety of
str
(string) classes. For example, you can use rjust
a method to get a string that is right-aligned at a certain width . Use help(str)
for more details.
Another common input/output type is processing files. The ability to create, read, and write files is necessary for many programs, and we'll explore how to implement them in this chapter.
File
You can file
open a file by creating an object of the class, using file
the class read
, readline
or method, write
to read and write the file appropriately. The ability to read and write to a file depends on the pattern you specify when you open the file. Finally, when you're done with the file, you call the close
method to tell Python that we're done with the file.
First, we create an instance of a class by indicating which files and schemas we want to open file
. The mode can be read mode ( ‘r‘
), write mode ( ‘w‘
), or Append mode ( ‘a‘
). In fact there are many more patterns available that you can use help(file)
to learn more about them.
We first open the file in write mode, then use the file
method of the class write
to write the file, and finally we use to close
close the file.
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 readline
the 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 print
use commas on the statement to eliminate the wrapping. Finally, we used to close
close this file.
Now, look at poem.txt
the contents of the file to verify that the program is working correctly.
Storage device
- Python provides a standard module, called the
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
, it functions and pickle
modules exactly the same, but it is written in C language , so much faster ( pickle
1000 times times faster).
You can use either of them, and we'll use the cPickle
module here. Remember, we put these two modules simply as pickle
modules.
The following is primarily a demonstration of usage, where the opening of a file should be written as
Try
With open (Shoplistfile, ' W ') as F:
P.dump (SHOPLIST,F)
Except Ioeerror as Ioerr:
Print "File error in (write to the file):" + str (IOERR)
First, notice 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 (or) by simply changing one line cPickle
pickle
! In the rest of the program, we simply refer to this module p
.
To store an object in a file, first open an object in write mode and file
then call the function of the memory module to dump
store the object in an open file. This process is called storage .
Next, we use the pickle
return of the load
function of the module to retrieve the object. This process is called fetching storage .
From:http://www.cnblogs.com/wei-li/archive/2012/03/26/2417311.html
Concise python Tutorial--c++ Programmer's Perspective (vi): input/Output IO