Python 批量压缩txt文件
时间: 2023-11-05 22:02:22 浏览: 131
以下是一个Python脚本,可以批量压缩指定文件夹中的所有txt文件:
```python
import os
import zipfile
# 定义要压缩的文件夹路径和压缩后的文件名
folder_path = 'path/to/folder'
zip_file_name = 'compressed.zip'
# 创建一个ZipFile对象,准备写入压缩文件
zip_file = zipfile.ZipFile(zip_file_name, 'w')
# 遍历指定文件夹中的所有txt文件
for filename in os.listdir(folder_path):
if filename.endswith('.txt'):
file_path = os.path.join(folder_path, filename)
# 将文件添加到压缩文件中
zip_file.write(file_path, filename)
# 关闭ZipFile对象
zip_file.close()
print(f'已将{folder_path}中的所有txt文件压缩为{zip_file_name}。')
```
将上述代码保存为一个.py文件,然后将`folder_path`和`zip_file_name`变量替换为实际的文件夹路径和压缩文件名,最后运行脚本即可。
相关问题
python 批量压缩图片大小
你可以使用Python中的Pillow库来批量压缩图片大小。
首先,你需要安装Pillow库。你可以使用以下命令来安装:
```
pip install Pillow
```
接下来,你可以使用以下代码来批量压缩图片大小:
```python
from PIL import Image
import os
def compress_images(directory=None, quality=30):
"""
压缩指定目录下的所有图片
:param directory: 图片所在的目录
:param quality: 压缩质量,默认为30
"""
if directory is None:
directory = os.getcwd()
print(f"压缩 {directory} 目录下的所有图片")
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png"):
filepath = os.path.join(directory, filename)
with Image.open(filepath) as im:
im.save(filepath, quality=quality)
print("完成压缩!")
# 示例
compress_images(directory="/path/to/directory", quality=50)
```
这个函数将遍历指定的目录,找到所有的 `.jpg`, `.jpeg`, 和 `.png` 文件,并将它们压缩到指定的质量级别。你可以根据需要调整 `quality` 参数的值来控制压缩质量。
请注意,这个函数将覆盖原始文件。如果你希望保留原始文件,请在保存压缩文件之前创建一个副本。
python批量压缩图片大小
你可以使用PIL库来批量压缩图片大小。以下是一个示例代码:
```python
from PIL import Image
import os
# 设置压缩后的图片大小
size = (512, 512)
# 设置原始图片路径
path = "original_images/"
# 设置压缩后图片保存路径
new_path = "compressed_images/"
# 遍历原始图片路径下的所有图片
for file_name in os.listdir(path):
# 如果不是图片文件,则跳过
if not file_name.endswith(".jpg") and not file_name.endswith(".jpeg") and not file_name.endswith(".png"):
continue
# 打开图片文件
image = Image.open(os.path.join(path, file_name))
# 压缩图片大小
image.thumbnail(size)
# 保存压缩后的图片
image.save(os.path.join(new_path, file_name))
```
在这个示例中,我们首先定义了压缩后的图片大小和原始图片路径和压缩后图片保存路径。然后,我们遍历原始图片路径下的所有图片,打开每个图片文件并压缩图片大小。最后,我们将压缩后的图片保存到压缩后图片保存路径中。
注意,如果原始图片不是正方形,那么压缩后的图片可能会出现变形。如果你想保持图片的比例,可以使用`resize()`函数而不是`thumbnail()`函数。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)