Extract certain files from .zip
Thanks to comment from @user20650.
Use two calls to unzip
. First with list=TRUE
just to get the $Name
for the files. Second with files=
to extract only the files whose names match the pattern.
zipped_csv_names <- grep('\\.csv$', unzip('some_archive.zip', list=TRUE)$Name,
ignore.case=TRUE, value=TRUE)
unzip('some_archive.zip', files=zipped_csv_names)
comb_tbl <- rbindlist(lapply(zipped_csv_names,
function(x) cbind(fread(x, sep=',', header=TRUE,
stringsAsFactors=FALSE),
file_nm=x)), fill=TRUE )
extract certain file from zip but not complete directory
import os
import shutil
import zipfile
zip_filepath='/home/sundeep/Desktop/SCHEMA AUTOMATION/SOURCE/DSP8010_2017.1.zip'
target_dir='/home/sundeep/Desktop/SCHEMA AUTOMATION/SCHEMA'
with zipfile.ZipFile(zip_filepath) as z:
with z.open('DSP8010_2017.1/json-schema/AccountService.json') as zf, open(os.path.join(target_dir, os.path.basename('AccountService.json')), 'wb') as f:
shutil.copyfileobj(zf, f)
How to extract specific set of files in a zip file using python
You can iterate over all files in zip archive and check filename before extracting:
import zipfile
with zipfile.ZipFile('test.zip', 'r') as zp:
files = zipfile.ZipFile.infolist(zp)
for file in files:
if file.filename.startswith('first'):
with open(file.filename, 'wb') as f:
f.write(zp.read(file.filename))
Related Topics
Implementation of Skyline Query or Efficient Frontier
Adding Multiple Columns in a Dplyr Mutate Call
Mutate Multiple/Consecutive Columns (With Dplyr or Base R)
Download Plotly Using Downloadhandler
How to Create a Bar and Line Plot with R Dygraphs
How to Capture the Output of System()
How to Retrieve the Client's Current Time and Time Zone When Using Shiny
Ggplot Legend - Scale_Colour_Manual Not Working
Install.Packages R on Ubuntu 12.04 Downloads But Does Not Install Packages
Compute Projection/Hat Matrix via Qr Factorization, Svd (And Cholesky Factorization)
Differencebetween Aes and Aes_String (Ggplot2) in R
Insert Missing Time Rows into a Dataframe
Histogram Conditional Fill Color
R - Svd() Function - Infinite or Missing Values in 'X'