简体   繁体   English

在 Python 中解压缩时如何为文件添加前缀?

[英]How to add prefix to the files while unzipping in Python?

Trying to add prefix to all the files in directory while unzipping in Python.尝试在 Python 中解压缩时为目录中的所有文件添加前缀。 Is there a way to add prefix to the file while unzipping?有没有办法在解压缩时为文件添加前缀?

Code:
====
import pathlib
import shutil
import os

file_path = "/emp/status/"
shutil.rmtree(file_path, ignore_errors=True)
pathlib.Path(file_path).mkdir(parents=True, exist_ok=True)
os.system("gzip -d " + file_path + "/*")


Output:
=======
data_0_1_0.csv
data_0_2_0.csv
data_0_3_0.csv


Required output:
================
emp_status_data_0_1_0.csv
emp_status_data_0_2_0.csv
emp_status_data_0_3_0.csv

Try this:尝试这个:

import os
for filename in pathlib.Path(path).glob('*.csv')
    dst = f"emp_status_{filename}"
    os.rename(filename,  os.path.join(file_path, dst))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM