Binyamin Even
Binyamin Even

Reputation: 3382

rename a zipped file in python

I have a zipped file. Inside of it I have a.tvx file - which I want to rename to .xml . So I tried the following: (of course, I imported all the relevant modules).

with zipfile.ZipFile(file_name) as z:
    for filename in z.namelist():
        if not os.path.isdir(filename):
             os.rename(filename,filename.replace("tvx","xml"))

and the error I got was:

WindowsError: [Error 2] The system cannot find the file specified

I thought that maybe the error was because the filename is not in absolute path,

so I tried also this:

with zipfile.ZipFile(complete_name) as z:
    for filename in z.namelist():
    if not os.path.isdir(filename):
        filename=os.path.abspath(filename) #making filename absolute path
        os.rename(filename,filename.replace("tvx","xml"))

but still, the same error.

Upvotes: 0

Views: 2369

Answers (1)

Ali Nikneshan
Ali Nikneshan

Reputation: 3502

You can't rename file within the zip file, So you should extract, rename and rezip the file.

Upvotes: 1

Related Questions