简体   繁体   中英

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. 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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