简体   繁体   中英

python replace string on files inside zipfile

I'm trying to replace a specific string inside files that are inside a zip file (and keep the zip folder structure and other files intact) using the following python code but I keep receiving errors such as: FileNotFoundError: [WinError 3] The system cannot find the path specified:

I'm still learning Python, so no I have no idea yet of whats is wrong

Does anyone have any idea? Thanks!

import zipfile

srcfile='test.zip'
dstfile='test2.zip'

with zipfile.ZipFile(srcfile) as inzip, zipfile.ZipFile(dstfile, "w") as outzip:
    for inzipinfo in inzip.infolist():
        with inzip.open(inzipinfo) as infile:
            if inzipinfo.filename.endswith('/') == False:
                if inzipinfo.filename.find("code.sas")>=0:
                    print(inzipinfo.filename)
                    content = infile.read()
                    content = content.replace("foo", "bar")
                    outzip.writestr(inzipinfo.filename, content)
                else:
                    content = infile.read()
                    outzip.write(inzipinfo.filename, content) #FileNotFoundError: [WinError 3]

EDIT: Done in C#, way easier

csc.exe /preferreduilang:en-US /target:exe /reference:System.IO.Compression.dll /reference:System.IO.dll /reference:System.IO.Compression.FileSystem.dll /out:program.exe program.cs

using System.IO.Compression;
using System.IO;

namespace ReplaceZipString
{
    class Program
    {
        static string tmpfolder = @"tmp\";

        static void Main(string[] args)
        {
            string indir = @"indir\";

            foreach (string infile in Directory.GetFiles(indir, "*.zip"))
            {
                DoFile(infile);
            }

        }

        private static void DoFile(string infile)
        {
            string outfile = infile;
            if (Directory.Exists(tmpfolder)) Directory.Delete(tmpfolder, true);

            ZipArchive inzip = ZipFile.OpenRead(infile);
            inzip.ExtractToDirectory(tmpfolder);
            inzip.Dispose();

            DirectoryInfo di = new DirectoryInfo(tmpfolder);
            foreach (FileInfo f in di.EnumerateFiles("code.sas", SearchOption.AllDirectories))
            {
                string text = File.ReadAllText(f.FullName, System.Text.Encoding.Default);
                text = text.Replace("foo", "bar");
                File.WriteAllText(f.FullName, text, System.Text.Encoding.Default);
            }

            if (File.Exists(outfile)) File.Delete(outfile);
            ZipFile.CreateFromDirectory(tmpfolder, outfile);
        }
    }
}

Made a few modifications, this works for me

import zipfile

srcfile = '..tmp//test.zip'
dstfile = '..tmp//test_out.zip'

inzip = zipfile.ZipFile(srcfile, 'r')
outzip = zipfile.ZipFile(dstfile, "w")

# Iterate the input files
for inzipinfo in inzip.infolist():

    print(inzipinfo)

    # Read input file
    infile = inzip.open(inzipinfo)

    if inzipinfo.filename == "test.txt":

        content = infile.read()

        new_content = str(content, 'utf-8').replace("abc", "123")

        # Write content
        outzip.writestr(inzipinfo.filename, new_content)

    else:  # Other file, dont want to modify => just copy it
        content = infile.read()
        outzip.writestr(inzipinfo.filename, content)

inzip.close()
outzip.close()

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