Skip to content

Commit fab51e2

Browse files
committed
13.1小节完成
1 parent c40f804 commit fab51e2

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

source/c12/p01_start_stop_thread.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import time
2020
def countdown(n):
2121
while n > 0:
22-
print('T-minus', n)
23-
n -= 1
24-
time.sleep(5)
22+
print('T-minus', n)
23+
n -= 1
24+
time.sleep(5)
2525
2626
# Create and launch a thread
2727
from threading import Thread

source/c13/p01_accept_input_via_redirect_pips_or_input_files.rst

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,55 @@
55
----------
66
问题
77
----------
8-
You want a script you’ve written to be able to accept input using whatever mechanism
9-
is easiest for the user. This should include piping output from a command to the script,
10-
redirecting a file into the script, or just passing a filename, or list of filenames, to the
11-
script on the command line.
8+
你希望你的脚本接受任何用户认为最简单的输入方式。包括将命令行的输出通过管道传递给该脚本、
9+
重定向文件到该脚本,或在命令行中传递一个文件名或文件名列表给该脚本。
1210

1311
|
1412
1513
----------
1614
解决方案
1715
----------
18-
Python’s built-in fileinput module makes this very simple and concise. If you have a
19-
script that looks like this:
20-
#!/usr/bin/env python3
21-
import fileinput
16+
Python内置的 ``fileinput`` 模块让这个变得简单。如果你有一个下面这样的脚本:
2217

23-
with fileinput.input() as f_input:
24-
for line in f_input:
25-
print(line, end='')
18+
.. code-block:: python
2619
27-
Then you can already accept input to the script in all of the previously mentioned ways.
28-
If you save this script as filein.py and make it executable, you can do all of the following
29-
and get the expected output:
20+
#!/usr/bin/env python3
21+
import fileinput
3022
31-
$ ls | ./filein.py # Prints a directory listing to stdout.
32-
$ ./filein.py /etc/passwd # Reads /etc/passwd to stdout.
33-
$ ./filein.py < /etc/passwd # Reads /etc/passwd to stdout.
23+
with fileinput.input() as f_input:
24+
for line in f_input:
25+
print(line, end='')
26+
27+
那么你就能以前面提到的所有方式来为此脚本提供输入。假设你将此脚本保存为 ``filein.py`` 并将其变为可执行文件,
28+
那么你可以像下面这样调用它,得到期望的输出:
29+
30+
.. code-block:: python
31+
32+
$ ls | ./filein.py # Prints a directory listing to stdout.
33+
$ ./filein.py /etc/passwd # Reads /etc/passwd to stdout.
34+
$ ./filein.py < /etc/passwd # Reads /etc/passwd to stdout.
3435
3536
|
3637
3738
----------
3839
讨论
3940
----------
40-
The fileinput.input() function creates and returns an instance of the FileInput
41-
class. In addition to containing a few handy helper methods, the instance can also be
42-
used as a context manager. So, to put all of this together, if we wrote a script that expected
43-
to be printing output from several files at once, we might have it include the filename
44-
and line number in the output, like this:
45-
46-
>>> import fileinput
47-
>>> with fileinput.input('/etc/passwd') as f:
48-
>>> for line in f:
49-
... print(f.filename(), f.lineno(), line, end='')
50-
...
51-
/etc/passwd 1 ##
52-
/etc/passwd 2 # User Database
53-
/etc/passwd 3 #
54-
55-
<other output omitted>
56-
57-
Using it as a context manager ensures that the file is closed when it’s no longer being
58-
used, and we leveraged a few handy FileInput helper methods here to get some extra
59-
information in the output.
41+
``fileinput.input()`` 创建并返回一个 ``FileInput`` 类的实例。
42+
该实例除了拥有一些有用的帮助方法外,它还可被当做一个上下文管理器使用。
43+
因此,整合起来,如果我们要写一个打印多个文件输出的脚本,那么我们需要在输出中包含文件名和行号,如下所示:
44+
45+
.. code-block:: python
46+
47+
>>> import fileinput
48+
>>> with fileinput.input('/etc/passwd') as f:
49+
>>> for line in f:
50+
... print(f.filename(), f.lineno(), line, end='')
51+
...
52+
/etc/passwd 1 ##
53+
/etc/passwd 2 # User Database
54+
/etc/passwd 3 #
55+
56+
<other output omitted>
57+
58+
通过将它作为一个上下文管理器使用,可以确保它不再使用时文件能自动关闭,
59+
而且我们在之类还演示了 ``FileInput`` 的一些有用的帮助方法来获取输出中的一些其他信息。

0 commit comments

Comments
 (0)