zipfile : zip only the files present in a directory - python-3.x

I want to zip just a file in Python which present in a folder. I am finding hard to create zip file with the below code snippet. It does create zip file, but it has complete folder structure inside.
import zipfile as zip
root=r"C:\XXXX\YYYYYY\ZZZZ\"
file="abc.txt"
zipper=zip.ZipFile(file=os.path.join(root,file.replace("txt","zip")),mode="w",compression=zip.ZIP_DEFLATED)
zipper.write(os.path.join(root,file))
zipper.close()
Actual output:
#################
abc.zip
|
XXXX - Folder
|
YYYYYY - Folder
|
ZZZZ - Folder
|
abc.txt
Expected output
###############
abc.zip
|
abc.txt

One way I learnt and working is :
os.chdir(root)
To set the working directory to the folder where the files are present. Then, pass just the filename instead of complete path to create zip.
Not sure, if it is the correct and best way.

Related

Extract zip file and keeping top folder using python

I have folder in like CW1234.zip and it has various folders and subfolders like below. So, CW1234.zip has CW_All folder which in turn has CW123 and CW234 folders and so on
CW1234.zip
CW_All
CW123
xyz.pdf
CW234
abc.doc
and to extract I use this code:
from zipfile import ZipFile
with ZipFile(r'CW41234.zip', 'r') as zipObj:
# Extract all the contents of zip file in current directory
zipObj.extract()
The only problem is the unzipped folder I get is from CW_All and all the subfolders and file.
What I want is to get it from CW1234 as one folder and then the structure follows?
Current Output
CW_All
CW123
xyz.pdf
CW234
abc.doc
Expected Output
CW1234
CW_All
CW123
xyz.pdf
CW234
abc.doc
Couldn't find anything in the documentation also!!
Using ZipFile.extractall() we can simply provide a new path to extract the contents of the archive to, which we can base on the filename of the archive.
I have a .zip file with the following structure:
archive1024.zip:.
│
└───Folder_with_script
stuff.py
Here is the script to extract all of the files inside of the archive into a sub-folder:
from zipfile import ZipFile
file = "archive1024.zip"
with ZipFile(file, "r") as zFile:
zFile.extractall(path=file.split(".")[0])
I now have a folder-structure like this:
J:.
│ archive1024.zip
│ unzip.py
│
└───archive1024
└───Folder_with_script
stuff.py

Script to extract only the MaxMind GeoLite2 Country database from gzip containing multiple files

Recently MaxMind changed their download policy, and the old simple format is no longer available. The new file format looks like this: GeoLite2-Country_20191231.tar.gz, and inside we have a folder with the same name containing two additional files.
Although there is an option to delete the date parameter from the link, it seems that the downloaded file will still contain the date.
Now, the problem is to extract that GeoLite2-Country.mmdb from the gzip file having that variable name programmatically.
The unzip part existing in my old script was this:
gunzip -c "$1"GeoLite2-Country.mmdb.gz > "$1"GeoLite2-Country.mmdb
The question is how to modify the above part for the new situation. Or, maybe someone knows another way to solve the same problem. Thanks in advance.
The folder structure:
-+ Geolite2-Country_YYYYMMDD.tar.gz:
|-+ Geolite2-Country_YYYYMMDD
|- licence.txt
|- copyright.txt
|- Geolite2-Country.mmdb
What I need is Geolite2-Country.mmdb in the current folder of gzip file.
tar -tf /GeoLite2-City.tar.gz | grep mmdb | xargs tar -xf /GeoLite2-City.tar.gz --strip-components 1 -C /
Just fix source and destination paths

Create zip files using spark(python)

I'm trying to create a zip file from several files. For example, I have 3 files
file1
file2
file3
I want to create a zip folder that contains all these files
Is there some way to create zip folder containing multiple files in Spark(Python)?
First of all spark is a framework which is also in python language. I did not understand how do you relate zip files and spark in this question.
If you want to create zip files in python, check out zipfiles library. Try and if you're stuck copy your code here. Then we will try to help.
from zipfile import ZipFile
# create a ZipFile object
with ZipFile('sampleDir.zip', 'w') as zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(dirName):
for filename in filenames:
#create complete filepath of file in directory
filePath = os.path.join(folderName, filename)
# Add file to zip
zipObj.write(filePath)
o/p:
sampleDir/file1.csv 2018-11-30 21:44:46 2829
sampleDir/file2.csv 2018-11-30 21:44:36 3386
sampleDir/file3.csv 2018-11-30 21:44:56 3552

How to rename multiple files while keeping extension based on provided txt file?

I have a folder with many files that look like:
A1_R1.fastq
A2_R1.fastq
A3_R1.fastq
I would like to rename the files based on a text file keeping the _R1.fastq but changing the A# to a specific samples name (example):
A1_R1.fastq KUG_R1.fastq
A2_R1.fastq AUG_R1.fastq
A3_R1.fastq TRY_R1.fastq
I'd also like an output directory which contains all my newly names .fastq files.
I tried this to no avail (only a few were renamed):
ls *.fastq| paste -d' ' - $PATH/txt | xargs -n2 mv
Thank you.

Getting file name from a list

So here is what i do already,
i have a abc.txt which contains list of files.Am using abc.txt to
move those files to a folder , tar that folder and finally i download the tar to local pc from server(linux).
it goes like
1.abc.txt
2.abc.txt(files) -> folder
3.Folder -> folder.tar
4.folder.tar -> local pc.
Now i need to change this like below,
if abc.txt contains 2 files namely,
example1.css
example2.css
i need to download those files from abc.txt seperately and directly to local pc ,
since ftp or sftp need the file name to download it how can i read that
from abc.txt.
Please help.
I think the hub of your problem is how to extract the correct files from your list for your subsequent two logic paths.
egrep 'example1.css|example2.css' abc.txt
will give you all lines that match the exceptions, and
egrep -v 'example1.css|example2.css' abc.txt
will give you all lines that don't match the exceptions

Resources