0

I'm trying to rename files before extraction using os.rename()

def unzip(date,day,zip_file):

        dest_dir = 'C:/Users/mbelahce040119/PycharmProjects/kpi/flats'

        with ZipFile(zip_file) as zipObj:
            files_sat = list()
            for zip_info in zipObj.infolist():
                if zip_info.filename[-1] == '/':
                    continue
                if zip_info.filename.startswith(date,42,50):
                    files_sat.append(zip_info.filename)
                    zip_info.filename = os.path.basename(max(files_sat))
            print(max(files_sat))
            zipObj.extract(max(files_sat), dest_dir)
            old_file = os.path.join(dest_dir,max(files_sat))
            new_file = os.path.join(dest_dir,date+'_D+'+day+'.csv')
            os.rename(old_file,new_file)

However, I don't know how to pass the new file name to the extract function and don't know if it should be before or after extraction. For now no extraction is happening because I changed the name in the line before.

mobelahcen
  • 414
  • 5
  • 22
  • 1
    You should rename it after the extraction. – accdias May 07 '19 at 10:37
  • 2
    If you are on Python 3.4 or higher, consider using [```pathlib```](https://docs.python.org/3/library/pathlib.html) instead of ```os.*``` for file operations. – accdias May 07 '19 at 10:48
  • for os, how can I do that? I tried to rename it after but doesn't work. I updated the question – mobelahcen May 07 '19 at 10:52
  • What actually didn't work? What happened instead of the thing you were expecting? Are you sure calling `max` without any key on a list of strings is what you want? – Mad Physicist May 07 '19 at 11:07
  • I guess the problem in your code is here: ```old_file = os.path.join(dest_dir,max(files_sat))```. You are storing the full file name in ```files_sat```, right? So, if you have a file name with a path info in your zip file, ```old_file``` with be set to ```dest_dir + old_path + filename```. – accdias May 07 '19 at 11:11
  • Yes `max` helped me select the most recent file among many updates since I had no timestamp. I posted an answer of what was the problem. Thanks – mobelahcen May 07 '19 at 11:12
  • I guess you will get a better result with ```sorted(files_sat).pop()``` instead of ```max()```. – accdias May 07 '19 at 11:14

2 Answers2

1

Here is an alternative version using pathlib:

from pathlib import Path
from zipfile import ZipFile


def unzip(date, day, zip_file):
    dest_dir = Path('C:/Users/mbelahce040119/PycharmProjects/kpi/flats')
    with ZipFile(zip_file) as zipObj:
        filelist = []
        for file in zipObj.infolist():
            if file.filename.startswith(date, 42, 50) and not file.is_dir():
                filelist.append(Path(file.filename))
        final_file = sorted(filelist).pop()
        zipObj.extract(final_file, dest_dir)
        final_file = dest_dir/final_file.name
        final_file.rename(dest_dir/f'{date}_D{day}.csv')

It is untested though since I wrote it based on your initial code.

accdias
  • 5,160
  • 3
  • 19
  • 31
0

I figured it out. The problem was the fact that I had already changed the filename to basename so I had to rename using the basename.

def unzip(date,day,zip_file):

        dest_dir = 'C:/Users/mbelahce040119/PycharmProjects/kpi/flats'

        with ZipFile(zip_file) as zipObj:
            files_sat = list()
            for zip_info in zipObj.infolist():
                if zip_info.filename[-1] == '/':
                    continue
                if zip_info.filename.startswith(date,42,50):
                    files_sat.append(zip_info.filename)
                    zip_info.filename = os.path.basename(max(files_sat))
            print(max(files_sat))
            final_file = max(files_sat)
            zipObj.extract(final_file, dest_dir)
            old_file = os.path.join(dest_dir,os.path.basename(final_file))
            new_file = os.path.join(dest_dir,date+'_D+'+day+'.csv')
            os.rename(old_file,new_file)
mobelahcen
  • 414
  • 5
  • 22