编辑1
嗯,我接受tar尊重空文件的答案......但在我的系统上:
$ touch emptytar
$ tar -tf emptytar
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
Run Code Online (Sandbox Code Playgroud)
也许我有一个非规范版本?
$ tar --version
tar (GNU tar) 1.22
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by John Gilmore and Jay Fenlason.
Run Code Online (Sandbox Code Playgroud)
大家好,
我正在测试一些逻辑来处理用户上传TAR文件.当我向tarfile.is_tarfile()它返回一个空白文件时返回True,这不是我所期望的:
$ touch tartest
$ cat tartest
$ python -c "import tarfile; print tarfile.is_tarfile('tartest')"
True
Run Code Online (Sandbox Code Playgroud)
如果我在文件中添加一些文本,它会返回False,这是我所期望的:
$ echo "not a tar" > tartest
$ python -c "import tarfile; print tarfile.is_tarfile('tartest')"
False
Run Code Online (Sandbox Code Playgroud)
我可以在开始添加一个检查,检查是否有长度为零的文件,但基于对文档的tarfile.is_tarfile(name),我认为这是不必要的:
如果name是tar归档文件,则返回True,tarfile模块可以读取.
我甚至检查了源码tarfile.py,我可以看到它正在检查标题块,但我不完全理解它是如何评估这些块的.
我误读了文档,因此设定了不公平的期望吗?
谢谢
Zachary
在命令行尝试这个:
$ touch emptyfile
$ tar -tvf emptyfile
Run Code Online (Sandbox Code Playgroud)
没有错误。
看起来空文件只是一个有效(但无用)的 TAR 文件。
| 归档时间: |
|
| 查看次数: |
1096 次 |
| 最近记录: |