我试图压缩test
文件夹的内容:
first.txt
pof/
pof/second.txt
如果我使用
cd
进入test
,然后使用zip进行压缩。zip -r folder.zip *
并使用以下命令检查生成的归档文件:
zipinfo folder.zip
我得到了这个输出:
Archive: folder.zip
Zip file size: 7573 bytes, number of entries: 3
-rw-r--r-- 3.0 unx 6473 tx defN 16-Mar-11 10:19 first.txt
drwxr-xr-x 3.0 unx 0 bx stor 16-Mar-11 10:20 pof/
-rw-r--r-- 3.0 unx 2841 tx defN 16-Mar-11 10:20 pof/second.txt
3 files, 9314 bytes uncompressed, 7113 bytes compressed: 23.6%
一切看起来都如预期般工作,但如果我使用zip压缩同一文件夹
shutil.make_archive('folder', 'zip', 'test')
然后使用以下命令检查存档:
zipinfo folder.zip
我得到了这个输出:
Archive: folder.zip
Zip file size: 7497 bytes, number of entries: 4
drwxr-xr-x 2.0 unx 0 b- defN 16-Mar-11 10:28 ./
drwxr-xr-x 2.0 unx 0 b- defN 16-Mar-11 10:20 pof/
-rw-r--r-- 2.0 unx 6473 b- defN 16-Mar-11 10:19 first.txt
-rw-r--r-- 2.0 unx 2841 b- defN 16-Mar-11 10:20 pof/second.txt
4 files, 9314 bytes uncompressed, 7113 bytes compressed: 23.6%
我不喜欢Python生成的zip归档文件中包含
./
,我该如何避免这种情况?
./
表示归档本身,即folder.zip
。当您在普通文件夹中时,可以通过相对路径./
引用它,例如/home/user/test/
与/home/user/test/./
或甚至/home/user/test/././././
相同。这只是一种指定列出目录内容时所列出的目录的方式。 - Nikitashutil.make_archive('folder', 'zip', 'test')
。我接受了你的编辑。 - LMGTFYzip -r
命令的输出)? - LMGTFYshutil.make_archive('folder', 'zip', 'test', '.')
的东西会有所帮助。 - Nikitaunzip folder.zip
解压 Python 生成的归档文件,点文件夹不会被创建,因为它已经存在,但我认为这不仅仅是一个显示问题。昨天我尝试指定base_dir
路径,但结果仍然相同。谢谢。 - LMGTFY