Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
04536b0e
Commit
04536b0e
authored
Jan 09, 2011
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#10871: "file" does not exist anymore in Python 3. Also adapt the reprs of opened file objects.
parent
c9007081
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
14 deletions
+14
-14
Doc/library/argparse.rst
Doc/library/argparse.rst
+14
-14
No files found.
Doc/library/argparse.rst
View file @
04536b0e
...
@@ -781,11 +781,11 @@ values are:
...
@@ -781,11 +781,11 @@ values are:
>>> parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
>>> parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
... default=sys.stdout)
... default=sys.stdout)
>>> parser.parse_args(['input.txt', 'output.txt'])
>>> parser.parse_args(['input.txt', 'output.txt'])
Namespace(infile=<
open file 'input.txt', mode 'r' at 0x...
>,
Namespace(infile=<
_io.TextIOWrapper name='input.txt' encoding='UTF-8'
>,
outfile=<
open file 'output.txt', mode 'w' at 0x...
>)
outfile=<
_io.TextIOWrapper name='output.txt' encoding='UTF-8'
>)
>>> parser.parse_args([])
>>> parser.parse_args([])
Namespace(infile=<
open file '<stdin>', mode 'r' at 0x...
>,
Namespace(infile=<
_io.TextIOWrapper name='<stdin>' encoding='UTF-8'
>,
outfile=<
open file '<stdout>', mode 'w' at 0x...
>)
outfile=<
_io.TextIOWrapper name='<stdout>' encoding='UTF-8'
>)
* ``'*'``. All command-line args present are gathered into a list. Note that
* ``'*'``. All command-line args present are gathered into a list. Note that
it generally doesn't make much sense to have more than one positional argument
it generally doesn't make much sense to have more than one positional argument
...
@@ -881,26 +881,26 @@ type
...
@@ -881,26 +881,26 @@ type
By default, ArgumentParser objects read command-line args in as simple strings.
By default, ArgumentParser objects read command-line args in as simple strings.
However, quite often the command-line string should instead be interpreted as
However, quite often the command-line string should instead be interpreted as
another type, like a :class:`float`
, :class:`int` or :class:`file`. The
another type, like a :class:`float`
or :class:`int`. The ``type`` keyword
``type`` keyword argument of :meth:`add_argument` allows any necessary
argument of :meth:`add_argument` allows any necessary type-checking and
type-c
hecking and type-conversions to be performed. Many common built-in types
type-c
onversions to be performed. Common built-in types and functions can be
can be
used directly as the value of the ``type`` argument::
used directly as the value of the ``type`` argument::
>>> parser = argparse.ArgumentParser()
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('foo', type=int)
>>> parser.add_argument('foo', type=int)
>>> parser.add_argument('bar', type=
file
)
>>> parser.add_argument('bar', type=
open
)
>>> parser.parse_args('2 temp.txt'.split())
>>> parser.parse_args('2 temp.txt'.split())
Namespace(bar=<
open file 'temp.txt', mode 'r' at 0x...
>, foo=2)
Namespace(bar=<
_io.TextIOWrapper name='temp.txt' encoding='UTF-8'
>, foo=2)
To ease the use of various types of files, the argparse module provides the
To ease the use of various types of files, the argparse module provides the
factory FileType which takes the ``mode=`` and ``bufsize=`` arguments of the
factory FileType which takes the ``mode=`` and ``bufsize=`` arguments of the
``file`` object
. For example, ``FileType('w')`` can be used to create a
:func:`open` function
. For example, ``FileType('w')`` can be used to create a
writable file::
writable file::
>>> parser = argparse.ArgumentParser()
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('bar', type=argparse.FileType('w'))
>>> parser.add_argument('bar', type=argparse.FileType('w'))
>>> parser.parse_args(['out.txt'])
>>> parser.parse_args(['out.txt'])
Namespace(bar=<
open file 'out.txt', mode 'w' at 0x...
>)
Namespace(bar=<
_io.TextIOWrapper name='out.txt' encoding='UTF-8'
>)
``type=`` can take any callable that takes a single string argument and returns
``type=`` can take any callable that takes a single string argument and returns
the type-converted value::
the type-converted value::
...
@@ -1512,7 +1512,7 @@ FileType objects
...
@@ -1512,7 +1512,7 @@ FileType objects
>>> parser = argparse.ArgumentParser()
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--output', type=argparse.FileType('wb', 0))
>>> parser.add_argument('--output', type=argparse.FileType('wb', 0))
>>> parser.parse_args(['--output', 'out'])
>>> parser.parse_args(['--output', 'out'])
Namespace(output=<
open file 'out', mode 'wb' at 0x...
>)
Namespace(output=<
_io.BufferedWriter name='out'
>)
FileType objects understand the pseudo-argument ``'-'`` and automatically
FileType objects understand the pseudo-argument ``'-'`` and automatically
convert this into ``sys.stdin`` for readable :class:`FileType` objects and
convert this into ``sys.stdin`` for readable :class:`FileType` objects and
...
@@ -1521,7 +1521,7 @@ FileType objects
...
@@ -1521,7 +1521,7 @@ FileType objects
>>> parser = argparse.ArgumentParser()
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('infile', type=argparse.FileType('r'))
>>> parser.add_argument('infile', type=argparse.FileType('r'))
>>> parser.parse_args(['-'])
>>> parser.parse_args(['-'])
Namespace(infile=<
open file '<stdin>', mode 'r' at 0x...
>)
Namespace(infile=<
_io.TextIOWrapper name='<stdin>' encoding='UTF-8'
>)
Argument groups
Argument groups
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment