[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版
for line in sys.stdin: sum += int(line) print(sum) Changing sorter to read line by line this way may not be a big performance bo ...
This technique generally works for Python scripts. Here, teststreams.py takes input from a file again. And, as in the last secti ...
key = msvcrt.getche() # use windows console tools msvcrt.putch(b'\n') # getch() does not echo key return key else: assert False, ...
import sys def getreply(): ?n But now the script also correctly pages text redirected into stdin from either a file or a command ...
Scripts use the preopened stdin and stdout file objects to access those sources, regard- less of their true nature. And for read ...
def init(self): self.text = '' # empty string when created def write(self, string): # add a string of bytes self.text += string ...
redirection utility function, we need to deal in terms of functions, not files). When run directly, the function reads from the ...
Enter a number>6 squared is 36 Enter a number>Bye This is an artificial example, of course, but the techniques illustrated ...
>>> sys.stdout = temp # restore original stream >>> buff.getvalue() '42 spam 3.141\n' Note that there is also ...
prints stuff to afile instead of to sys.stdout. The net effect is similar to simply as- signing sys.stdout to an object, but the ...
Redirecting input or output with os.popen In fact, by passing in the desired mode flag, we redirect either a spawned program’s o ...
Redirecting input and output with subprocess For even more control over the streams of spawned programs, we can employ the subpr ...
In fact, we can use obtain both the input and output streams of a spawned program with this module. Let’s reuse the simple write ...
We can get close to this with os.popen, but that the fact that its pipes are read or write (and not both) prevents us from catch ...
Python Versus csh If you are familiar with other common shell script languages, it might be useful to see how Python compares. H ...
...
CHAPTER 4 File and Directory Tools “Erase Your Hard Drive in Five Easy Steps!” This chapter continues our look at system interfa ...
SQL databases (sqlite3 and third-party add-ons). The last two of these categories are related to database topics, addressed in C ...
and the xml package in Unicode str. You must even be aware of the 3.X text/binary distinction when using system tools like pipe ...
to close the file. File write calls return the number of characters or bytes written (which we’ll sometimes omit in this book to ...
«
4
5
6
7
8
9
10
11
12
13
»
Free download pdf