“好事”文章分享文章名《【Python爬虫五十个小案例】爬取中国天气网城市天气》 作者:小馒头学Python

评价:

本文详细介绍了使用Python进行天气数据爬取的全过程,从准备工具到代码实现,再到数据保存,内容详实且实用性强。非常适合初学者学习和实践。

文章链接:https://cloud.tencent.com/developer/article/2472933

一、gzip命令简介gzip 是 Linux 系统中一个非常常用的命令,用来压缩文件。它使用 Lempel-Ziv 编码 (LZ77) 和霍夫曼编码的组合来减少文件大小。gzip 命令创建的文件通常以 .gz 作为扩展名。

二、gzip命令使用帮助2.1 help帮助信息使用--help选项,查询gzip命令的基本帮助信息。

代码语言:bash复制[root@openEuler-test ~]# gzip --help

Usage: gzip [OPTION]... [FILE]...

Compress or uncompress FILEs (by default, compress FILES in-place).

Mandatory arguments to long options are mandatory for short options too.

-c, --stdout write on standard output, keep original files unchanged

-d, --decompress decompress

-f, --force force overwrite of output file and compress links

-h, --help give this help

-k, --keep keep (don't delete) input files

-l, --list list compressed file contents

-L, --license display software license

-n, --no-name do not save or restore the original name and timestamp

-N, --name save or restore the original name and timestamp

-q, --quiet suppress all warnings

-r, --recursive operate recursively on directories

--rsyncable make rsync-friendly archive

-S, --suffix=SUF use suffix SUF on compressed files

--synchronous synchronous output (safer if system crashes, but slower)

-t, --test test compressed file integrity

-v, --verbose verbose mode

-V, --version display version number

-1, --fast compress faster

-9, --best compress better

With no FILE, or when FILE is -, read standard input.

Report bugs to .2.2 选项解释语法代码语言:bash复制gzip(选项)(参数)选项解释信息参数

描述

-c, --stdout

将结果输出到标准输出,保持原始文件不变。

-d, --decompress

解压缩。

-f, --force

强制覆盖输出文件并压缩链接。

-h, --help

显示此帮助信息。

-k, --keep

保留(不删除)输入文件。

-l, --list

列出压缩文件的内容。

-L, --license

显示软件许可证。

-n, --no-name

不保存或恢复原始文件名和时间戳。

-N, --name

保存或恢复原始文件名和时间戳。

-q, --quiet

抑制所有警告。

-r, --recursive

对目录递归操作。

--rsyncable

创建适合 rsync 的存档。

-S, --suffix=SUF

在压缩文件上使用后缀 SUF。

--synchronous

同步输出(如果系统崩溃更安全,但速度较慢)。

-t, --test

测试压缩文件的完整性。

-v, --verbose

详细模式。

-V, --version

显示版本号。

-1, --fast

更快地压缩。

-9, --best

更好地压缩。

三、gzip命令的基本使用3.1 压缩文件我们创建测试目录与测试文件。代码语言:bash复制[root@openEuler-test ~]# mkdir /test && cd /test

[root@openEuler-test test]# ls

[root@openEuler-test test]# touch file{01..10}

[root@openEuler-test test]# ls

file01 file02 file03 file04 file05 file06 file07 file08 file09 file10

[root@openEuler-test test]# echo aatest > /test/file01要压缩一个或多个文件,我们可以直接使用以下命令:代码语言:bash复制[root@openEuler-test test]# gzip file01

[root@openEuler-test test]# ll

total 4

-rw-r--r-- 1 root root 34 Sep 29 09:17 file01.gz

-rw-r--r-- 1 root root 0 Sep 29 09:16 file02

-rw-r--r-- 1 root root 0 Sep 29 09:16 file03

-rw-r--r-- 1 root root 0 Sep 29 09:16 file04

-rw-r--r-- 1 root root 0 Sep 29 09:16 file05

-rw-r--r-- 1 root root 0 Sep 29 09:16 file06

-rw-r--r-- 1 root root 0 Sep 29 09:16 file07

-rw-r--r-- 1 root root 0 Sep 29 09:16 file08

-rw-r--r-- 1 root root 0 Sep 29 09:16 file09

-rw-r--r-- 1 root root 0 Sep 29 09:16 file103.2 保留原始文件如果我们想保留原始文件,使用-k选项。

代码语言:bash复制[root@openEuler-test test]# gzip -k file02

[root@openEuler-test test]# ll

total 8

-rw-r--r-- 1 root root 34 Sep 29 09:17 file01.gz

-rw-r--r-- 1 root root 0 Sep 29 09:16 file02

-rw-r--r-- 1 root root 27 Sep 29 09:16 file02.gz

-rw-r--r-- 1 root root 0 Sep 29 09:16 file03

-rw-r--r-- 1 root root 0 Sep 29 09:16 file04

-rw-r--r-- 1 root root 0 Sep 29 09:16 file05

-rw-r--r-- 1 root root 0 Sep 29 09:16 file06

-rw-r--r-- 1 root root 0 Sep 29 09:16 file07

-rw-r--r-- 1 root root 0 Sep 29 09:16 file08

-rw-r--r-- 1 root root 0 Sep 29 09:16 file09

-rw-r--r-- 1 root root 0 Sep 29 09:16 file103.3 解压文件使用-d选项,进行解压文件。

代码语言:bash复制[root@openEuler-test test]# gzip -d file01.gz

[root@openEuler-test test]# ll

total 8

-rw-r--r-- 1 root root 7 Sep 29 09:17 file01

-rw-r--r-- 1 root root 0 Sep 29 09:16 file02

-rw-r--r-- 1 root root 27 Sep 29 09:16 file02.gz

-rw-r--r-- 1 root root 0 Sep 29 09:16 file03

-rw-r--r-- 1 root root 0 Sep 29 09:16 file04

-rw-r--r-- 1 root root 0 Sep 29 09:16 file05

-rw-r--r-- 1 root root 0 Sep 29 09:16 file06

-rw-r--r-- 1 root root 0 Sep 29 09:16 file07

-rw-r--r-- 1 root root 0 Sep 29 09:16 file08

-rw-r--r-- 1 root root 0 Sep 29 09:16 file09

-rw-r--r-- 1 root root 0 Sep 29 09:16 file103.4 查看压缩信息查看压缩比率::使用 -l 选项可以列出每个被压缩文件的信息,包括未压缩的大小、压缩后的大小、压缩比等信息

代码语言:bash复制[root@openEuler-test test]# gzip -l file01.gz

compressed uncompressed ratio uncompressed_name

34 7 -28.6% file013.5 标准输出/输入保留原始文件,把压缩流重定向到新文件代码语言:bash复制gzip -c file03 > file01.gz保留原始文件,把解压流重定向到新文件代码语言:bash复制gzip -dc file01.gz > file033.6 批量处理文件可以一次性压缩多个文件

代码语言:bash复制gzip file02 file03 file043.7 递归解压缩目录重新创建测试目录和文件代码语言:bash复制[root@openEuler-test test]# mkdir -p test01/test03

[root@openEuler-test test]# echo aa > test01/test03/aa.txt

[root@openEuler-test test]# echo bb > test01/bb.txt递归进行压缩目录:所有test01目录下面的文件都变成了.gz,目录依然存在只是目录里面的文件相应变成了.gz.这就是压缩,和打包不同。因为是对目录操作,所以需要加上-r选项,这样也可以对子目录进行递归了。

代码语言:bash复制[root@openEuler-test test]# gzip -rv test01/

test01/test03/aa.txt: -66.7% -- replaced with test01/test03/aa.txt.gz

test01/bb.txt: -66.7% -- replaced with test01/bb.txt.gz

[root@openEuler-test test]# ll

total 4

drwxr-xr-x 3 root root 4096 Sep 29 09:42 test01

[root@openEuler-test test]# tree ./

./

└── test01

├── bb.txt.gz

└── test03

└── aa.txt.gz

3 directories, 2 files递归进行解压目录代码语言:bash复制[root@openEuler-test test]# gzip -dr test01

[root@openEuler-test test]# tree ./

./

└── test01

├── bb.txt

└── test03

└── aa.txt

3 directories, 2 files3.8测试压缩文件完整性使用 -t 选项可以检查 .gz 文件是否完整且没有损坏

代码语言:bash复制gzip -t file01.txt.gz四、注意事项使用 gzip 会替换原始文件,除非你加上 -k 选项来保留原文件。在压缩多个文件或目录时,gzip 不支持直接压缩整个目录,需要先将目录打包成一个文件(如通过 tar)再进行压缩。指定不同的压缩级别会影响压缩速度和最终文件大小,通常默认的6级已经足够日常使用。当解压文件时确保有足够的磁盘空间来存放解压后的数据,因为解压后文件可能显著增大。如果要检查 .gz 文件的完整性,可以使用 gzip -t 选项避免在解压损坏文件时丢失数据。对于非常大的文件,考虑使用更高级别的压缩可能会大幅增加处理时间,需根据实际需求权衡。使用通配符批量压缩文件前,请确认不会意外地压缩不需要的文件。