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
84f6de9d
Commit
84f6de9d
authored
Feb 13, 2007
by
Martin v. Löwis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Patch #1517891: Make 'a' create the file if it doesn't exist.
Fixes #1514451.
parent
c6d626ed
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
1 deletion
+38
-1
Doc/lib/libzipfile.tex
Doc/lib/libzipfile.tex
+5
-0
Lib/test/test_zipfile.py
Lib/test/test_zipfile.py
+22
-0
Lib/zipfile.py
Lib/zipfile.py
+8
-1
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/lib/libzipfile.tex
View file @
84f6de9d
...
...
@@ -101,6 +101,8 @@ cat myzip.zip >> python.exe
\end{verbatim}
also works, and at least
\program
{
WinZip
}
can read such files.
If
\var
{
mode
}
is
\code
{
a
}
and the file does not exist at all,
it is created.
\var
{
compression
}
is the ZIP compression method to use when writing
the archive, and should be
\constant
{
ZIP
_
STORED
}
or
\constant
{
ZIP
_
DEFLATED
}
; unrecognized values will cause
...
...
@@ -114,6 +116,9 @@ cat myzip.zip >> python.exe
ZIP file would require ZIP64 extensions. ZIP64 extensions are disabled by
default because the default
\program
{
zip
}
and
\program
{
unzip
}
commands on
\UNIX
{}
(the InfoZIP utilities) don't support these extensions.
\versionchanged
[If the file does not exist, it is created if the
mode is 'a']
{
2.6
}
\end{classdesc}
\begin{methoddesc}
{
close
}{}
...
...
Lib/test/test_zipfile.py
View file @
84f6de9d
...
...
@@ -307,6 +307,28 @@ class PyZipFileTests(unittest.TestCase):
class
OtherTests
(
unittest
.
TestCase
):
def
testCreateNonExistentFileForAppend
(
self
):
if
os
.
path
.
exists
(
TESTFN
):
os
.
unlink
(
TESTFN
)
filename
=
'testfile.txt'
content
=
'hello, world. this is some content.'
try
:
zf
=
zipfile
.
ZipFile
(
TESTFN
,
'a'
)
zf
.
writestr
(
filename
,
content
)
zf
.
close
()
except
IOError
,
(
errno
,
errmsg
):
self
.
fail
(
'Could not append data to a non-existent zip file.'
)
self
.
assert_
(
os
.
path
.
exists
(
TESTFN
))
zf
=
zipfile
.
ZipFile
(
TESTFN
,
'r'
)
self
.
assertEqual
(
zf
.
read
(
filename
),
content
)
zf
.
close
()
os
.
unlink
(
TESTFN
)
def
testCloseErroneousFile
(
self
):
# This test checks that the ZipFile constructor closes the file object
# it opens if there's an error in the file. If it doesn't, the traceback
...
...
Lib/zipfile.py
View file @
84f6de9d
...
...
@@ -396,7 +396,14 @@ class ZipFile:
self
.
_filePassed
=
0
self
.
filename
=
file
modeDict
=
{
'r'
:
'rb'
,
'w'
:
'wb'
,
'a'
:
'r+b'
}
self
.
fp
=
open
(
file
,
modeDict
[
mode
])
try
:
self
.
fp
=
open
(
file
,
modeDict
[
mode
])
except
IOError
:
if
mode
==
'a'
:
mode
=
key
=
'w'
self
.
fp
=
open
(
file
,
modeDict
[
mode
])
else
:
raise
else
:
self
.
_filePassed
=
1
self
.
fp
=
file
...
...
Misc/NEWS
View file @
84f6de9d
...
...
@@ -128,6 +128,9 @@ Core and builtins
Library
-------
- Patch #1517891: Mode '
a
' for ZipFile now creates the file if it
doesn'
t
exist
.
-
Patch
#
698833
:
Support
file
decryption
in
zipfile
.
-
Patch
#
685268
:
Consider
a
package
's __path__ in imputil.
...
...
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