[英]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:我正在尝试使用以下 pythonError 代码替换 zip 文件中的特定字符串(并保持 zip 文件夹结构和其他文件完好无损),但我一直收到错误,例如:FileNotFound找不到指定的路径:
I'm still learning Python, so no I have no idea yet of whats is wrong我还在学习 Python,所以不,我还不知道出了什么问题
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编辑:在 C# 中完成,更容易
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 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.