|
5 | 5 | ----------
|
6 | 6 | 问题
|
7 | 7 | ----------
|
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 | +重定向文件到该脚本,或在命令行中传递一个文件名或文件名列表给该脚本。 |
12 | 10 |
|
13 | 11 | |
|
14 | 12 |
|
15 | 13 | ----------
|
16 | 14 | 解决方案
|
17 | 15 | ----------
|
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`` 模块让这个变得简单。如果你有一个下面这样的脚本: |
22 | 17 |
|
23 |
| -with fileinput.input() as f_input: |
24 |
| - for line in f_input: |
25 |
| - print(line, end='') |
| 18 | +.. code-block:: python |
26 | 19 |
|
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 |
30 | 22 |
|
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. |
34 | 35 |
|
35 | 36 | |
|
36 | 37 |
|
37 | 38 | ----------
|
38 | 39 | 讨论
|
39 | 40 | ----------
|
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