打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Chapter 5: Input and Output

Chapter 5: Input and Output

A program means very little if it does not take input of some kind from theprogram user. Likewise, if there is no form of output from a program then onemay ask why we have a program at all. Input and output operations can definethe user experience and usability of any program. This chapter is all about howto put information or data into a program, and then how to display it or saveit to a file. This chapter does not discuss working with databases, but rather,working at a more rudimentary level with files. Throughout this chapter youwill learn such techniques as how to input data for a program via a terminal orcommand line, likewise, you will learn how to read input from a file and writeto a file. After reading this chapter, you should know how to persist Pythonobjects to disk using the pickle module and also how to retrieve objects fromdisk and use them.

Input from the Keyboard

As stated, almost every program takes input from a user in one form or another.Most basic applications allow for keyboard entry via a terminal or command lineenvironment. Python makes keyboard input easy, and as with many othertechniques in Python there are more than one way to enable keyboard input. Inthis section, we’ll cover each of those different ways to perform this task,along with a couple of use-cases. In the end you should be able to identify themost suitable method of performing input and output for your needs.

sys.stdin and raw_input

Making use of std.stdin is by far the most widely used method to read inputfrom the command line or terminal. This procedure consists of importing the syspackage, then writing a message prompting the user for some input, and lastlyreading the input by making a call to sys.stdin.readln() and assigning thereturned value to a variable. The process looks like the code that is displayedin Listing 5-1.

Listing 5-1. Using sys.stdin

# Obtain a value from the command line and store it into a variable>>> import sys>>> fav_team = sys.stdin.readline()Cubs>>> sys.stdout.write("My favorite team is: %s" % fav_team)My favorite team is: Cubs

You can see that the usage of sys modules is quite easy. However, anotherapproach to performing this same task is to make use of the raw_inputfunction. This function uses a more simplistic syntax in order to perform thesame procedure. It basically generates some text on the command line orterminal, accepts user input, and assigns it to a variable. Let’s take a lookat the same example from above using the raw_input syntax. Note that there isanother function that performs a similar task named the input function.However, the input function needs to be used with great care as it could be apotential security risk. The raw_input function always returns content passedin as a string whereas the input function returns content and evaluates it asan expression. It is safest to stay away from using input whenever possible.

Listing 5-2. Using raw_input

# Obtain a value using raw_input and store it into a variable>>> fav_team = raw_input("Enter your favorite team: ")Enter your favorite team: Cubs

Obtaining Variables from Jython Environment

It is possible to retrieve values directly from the Jython environment for usewithin your applications. For instance, we can obtain system environmentvariables or the strings that have been passed into the command line orterminal when running the program.

To use environment variable values within your Jython application, simplyimport the os module and use it’s environ dictionary to access them. Since thisis a dictionary object, you can obtain a listing of all environment variablesby simply typing os.environ.

Listing 5-3. Obtaining and Altering System Environment Variables

>>> import os>>> os.environ["HOME"]'/Users/juneau'# Change home directory for the Python session>>> os.environ["HOME"] = "/newhome">>> os.environ["HOME"]/newhome'

When you are executing a Jython module from the command prompt or terminal, youcan make use of the sys.argv list that takes values from the command prompt orterminal after invoking the Jython module. For instance, if we are interestedin having our program user enter some arguments to be used by the module, theycan simply invoke the module and then type all of the text entries followed byspaces, using quotes if you wish to pass an argument that contains a space. Thenumber of arguments can be any size (I’ve never hit an upper bound anyways), sothe possibilities are endless.

Listing 5-4. Using sys.argv

# sysargv_print.py – Prints all of the arguments provided at the command lineimport sysfor sysargs in sys.argv:    print sysargs# Usage>>> jython sysargv_print.py test test2 "test three"sysargv_print.pytesttest2test three

As you can see, the first entry in sys.argv is the script name, and then eachadditional argument provided after the module name is then added to thesys.argv list. This is quite useful for creating scripts to use for automatingtasks, etc.

File I/O

You learned a bit about the File data type in Chapter 2. In that chapter, webriefly discussed a few of the operations that can be performed using thistype. In this section, we will go into detail on what we can do with a Fileobject. We’ll start with the basics, and move into more detail. To begin, youshould take a look at Table 5-1 that lists all of the methods available to aFile object and what they do.

Table 5-1. File Object Methods

MethodDescription
close()Close file
fileno()Returns integer file descriptor
flush()Used to flush or clear the output buffers and write contentto the file
isatty()If the file is an interactive terminal, returns 1
next()This allows the file to be iterated over. Returns the nextline in the file. If no line is found, raises StopIteration
read(x)Reads x bytes
readline(x)Reads single line up to x characters, or entire line if x isomitted
readlines(size)Reads all lines in file into a list. If size > 0, reads thatnumber of characters
seek()Moves cursor to a new position in the file
tell()Returns the current position of the cursor
truncate(size)Truncates file’s size. Size defaults to current positionunless specified
write(string)Writes a string to the file object
writelines(seq)Writes all strings contained in a sequence with no separator

We’ll start by creating a file for use. As discussed in Chapter 2, theopen(filename[, mode]) built-in function creates and opens a specified file ina particular manner. The mode specifies what mode we will open the file into,be it read, read-write, and so on.

Listing 5-5. Creating, Opening, and Writing to a File

>>> my_file = open('mynewfile.txt','w')>>> first_string = "This is the first line of text.">>> my_file.write(first_string)>>> my_file.close()

In this example, the file “mynewfile.txt” did not exist until the open functionwas called. If it did exist already, the previous version is overwritten by thenew version and it is now empty. The file was created in write mode and then wedo just that, write a string to the file. Now, it is important to make mentionthat the first_string is not actually written to the file until it is closed orflush() is performed. It is also worth mentioning that if we were to close thefile, reopen it, and perform a subsequent write() operation on the file thenthe previous contents of the file would be overwritten by content of the newwrite.

Now we’ll step through each of the file functions in an example. The main focusof this example is to provide you with a place to look for actual working fileI/O code.

Listing 5-6.

# Write lines to file, flush, and close>>> my_file = open('mynewfile.txt','w')>>> my_file.write('This is the first line of text.\n')>>> my_file.write('This is the second line of text.\n')>>> my_file.write('This is the last line of text.\n')>>> my_file.flush()  # Optional, really unneccesary if closing the file but useful to clear>>>          #buffer>>> my_file.close()# Open file in read mode>>> my_file = open('mynewfile.txt','r')>>> my_file.read()'This is the first line of text.\nThis is the second line of text.\nThis is the last line of text.\n'# If we read again, we get a '' because cursor is at the end of text>>> my_file.read()''# Seek back to the beginning of file and perform read again>>> my_file.seek(0)>>> my_file.read()'This is the first line of text.This is the second line of text.This is the last line of text.'# Seek back to beginning of file and perform readline()>>> my_file.seek(0)>>> my_file.readline()'This is the first line of text.\n'>>> my_file.readline()'This is the second line of text.\n'>>> my_file.readline()'This is the last line of text.\n'>>> my_file.readline()''# Use tell() to display current cursor position>>> my_file.tell()93L>>> my_file.seek(0)>>> my_file.tell()0L# Loop through lines of file>>> for line in my_file:...     print line...This is the first line of text.This is the second line of text.This is the last line of text.

There are a handful of read-only attributes that we can use to find out moreinformation about file objects. For instance, if we are working with a file andwant to see if it is still open or if it has been closed, we could view theclosed attribute on the file to return a boolean stating whether the file isclosed. Table 5-2 lists each of these attributes and what they tell us about afile object.

Table 5-2. File Attributes

AttributeDescription
closedReturns a boolean to indicate if the file is closed
encodingReturns a string indicating encoding on file
modeReturns the I/O mode for a file(i.e., ‘r’, ‘w’, ‘r+,’rb’, etc.)
nameReturns the name of the file
newlinesReturns the newline representation in the file. This keeps track ofthe types of newlines encountered while reading the file. Allowsfor universal newline support.

Listing 5-7. File Attribute Usage

>>> my_file.closedFalse>>> my_file.mode'r'>>> my_file.name'mynewfile.txt'

Pickle

One of the most popular modules in the Python language is the pickle module.The goal of this module is basically to allow for the serialization andpersistence of Python objects to disk in file format. A pickled object can bewritten to disk using this module, and it can also be read back in and utilizedin object format. Just about any Python object can be persisted using pickle.

To write an object to disk, we call the pickle() function. The object will bewritten to file in a format that may be unusable by anything else, but we canthen read that file back into our program and use the object as it was prior towriting it out. In the following example, we’ll create a Player object and thenpersist it to file using pickle. Later, we will read it back into a program andmake use of it. We will make use of the File object when working with thepickle module.

Listing 5-8. Write an Object to Disk Using Pickle

>>> import pickle>>> class Player(object):...     def __init__(self, first, last, position):...         self.first = first...         self.last = last...         self.position = position...>>> player = Player('Josh','Juneau','Forward')>>> pickle_file = open('myPlayer','wb')>>> pickle.dump(player, pickle_file)>>> pickle_file.close()

In the example above, we’ve persisted a Player object to disk using thedump(object, file) method in the pickle module. Now let’s read the object backinto our program and print it out.

Listing 5-9. Read and Use a Pickled Object

>>> pickle_file = open('myPlayer','rb')>>> player1 = pickle.load(pickle_file)>>> pickle_file.close()>>> player1.first'Josh'>>> player1.last, player1.position('Juneau', 'Forward')

Similarly, we read the pickled file back into our program using the load(file)method. Once read and stored into a variable, we can close the file and workwith the object. If we had to perform a sequence of dump or load tasks, wecould do so one after the other without issue. You should also be aware thatthere are different pickle protocols that can be used in order to make picklework in different Python environments. The default protocol is 0, but protocols1 and 2 are also available for use. It is best to stick with the default as itworks well in most situations, but if you run into any trouble using picklewith binary formats then please give the others a try.

If we had to store objects to disk and reference them at a later time, it maymake sense to use the shelve module which acts like a dictionary for pickledobjects. With the shelve technique, you basically pickle an object and store itusing a string-based key value. You can later retrieve the object by passingthe key to the opened file object. This technique is very similar to a filingcabinet for our objects in that we can always reference our objects by keyvalue. Let’s take a look at this technique and see how it works.

Listing 5-10. Using the Shelve Technique

# Store different player objects>>> import shelve>>> player1 = Player('Josh','Juneau','forward')>>> player2 = Player('Jim','Baker','defense')>>> player3 = Player('Frank','Wierzbicki','forward')>>> player4 = Player('Leo','Soto','defense')>>> player5 = Player('Vic','Ng','center')>>> data = shelve.open("players")>>> data['player1'] = player1>>> data['player2'] = player2>>> data['player3'] = player3>>> data['player4'] = player4>>> data['player5'] = player5>>> player_temp = data['player3']>>> player_temp.first, player_temp.last, player_temp.position('Frank', 'Wierzbicki', 'forward')>>> data.close()

In the scenario above, we used the same Player object that was defined in theprevious examples. We then opened a new shelve and named it “players”, thisshelve actually consists of a set of three files that are written to disk.These three files can be found on disk named “players.bak”, “players.dat”, and“players.dir” once the objects were persisted into the shelve and when close()was called on the object. As you can see, all of the Player objects we’veinstantiated have all been stored into this shelve unit, but they exist underdifferent keys. We could have named the keys however we wished, as long as theywere each unique. In the example, we persist five objects and then, at the end,one of the objects is retrieved and displayed. This is quite a nice techniqueto make a small data store.

Output Techniques

We basically covered the print statement in Chapter 2 very briefly whendiscussing string formatting. The print statement is by far the most utilizedform of output in most Python programs. Although we covered some basics such asconversion types and how to format a line of output in Chapter 2, here we willgo into a bit more depth on some different variations of the print statement aswell as other techniques for generating output. There are basically two formatsthat can be used with the print statement. We covered the first in Chapter 2,and it makes use of a string and some conversion types embedded within thestring and preceded by a percent (%) symbol. After the string, we use anotherpercent(%) symbol followed by a parenthesized list of arguments that will besubstituted in place of the embedded conversion types in our string in order.Check out the examples of each depicted in the example below.

Listing 5-11. Output With the Print Statement

# Using the % symbol>>> x = 5>>> y = 10>>> print 'The sum of %d and %d is %d' % (x, y, (x + y))The sum of 5 and 10 is 15>>> adjective = "awesome">>> print 'Jython programming is %s' % (adjective)Jython programming is awesome

You can also format floating-point output using the conversion types that areembedded in your string. You may specify a number of decimal places you’d liketo print by using a “.# of places” syntax in the embedded conversion type.

Listing 5-12. Formatting Floating-Point Arithmetic

>>> pi = 3.14>>> print 'Here is some formatted floating point arithmetic: %.2f' % (pi + y)Here is some formatted floating point arithmetic: 13.14>>> print 'Here is some formatted floating point arithmetic: %.3f' % (pi + y)Here is some formatted floating point arithmetic: 13.140

Summary

It goes without saying that Python has its share of input and outputstrategies. This chapter covered most of those techniques starting with basicterminal or command line I/O and then onto file manipulation. We learned how tomake use of the open function for creating, reading, or writing a file. Thecommand line sys.argv arguments are another way that we can grab input, andenvironment variables can also be used from within our programs. Followingthose topics, we took a brief look at the pickle module and how it can be usedto persist Python objects to disk. The shelve module is another twist on usingpickle that allows for multiple objects to be indexed and stored within thesame file. Finally, we discussed a couple of techniques for performing outputin our programs.

Although there are some details that were left out as I/O could consume anentire book, this chapter was a solid starting point into the broad topic ofI/O in Python. As with much of the Python language specifics discussed in thisbook, there are many resources available on the web and in book format thatwill help you delve deeper into the topics if you wish. A good resource isBeginning Python: From Novice to Professional by: Magnus Lie Hetland. You mayalso wish to look at the Python documentation which can be found atwww.python.org/doc/.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
成功解决ForkingPickler(file, protocol).dump(obj) TypeError: can't pickle Environment objects
python储存器
【转】Python之Pickle模块(持久化对象存储)
[Python]第一个简单的python实例
python中的open函数
How Does a CPU Work?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服