zipfile.ZipFile creating an archive without file paths - zip

I create an empty archive and write files there.
import zipfile #include the module
zname=r'D:\bdseoru.zip' # create variable - file name and location
newzip=zipfile.ZipFile(zname,'w') #create archive
newzip.write(r'D:\1\milion.txt') #add file to archive
newzip.write(r'D:\1\links.txt') #add file2 to archive
newzip.close() #close the archive
If I unzip this archive, I get files that are in folder 1. How to pack only files (milion.txt links.txt) into the archive so that there is no folder 1 in the archive.

Related

Renaming Files in Subdirectories using file path

Scenario: I am trying to Rename all .txt file named "a.txt" in all subfolders of a directory.
Question: I came up with the following code, but it has and issue: My loops don't work as expected, I was hoping to get the directory loop, to use the last part of the path, and use that string to rename the file. Right now, my code will rename the file with the latest directory name. How can this be fixed?
Code:
import os
import fnmatch
directory = "C:/Users/DGMS/Desktop/Test"
for root, subdirectories, files in os.walk(directory):
for subdirectory in subdirectories:
pathtest = os.path.basename(os.path.normpath(os.path.join(root, subdirectory)))
print(pathtest)
for file in files:
if fnmatch.fnmatch(file, 'a.txt'):
os.rename(os.path.join(root, file),(os.path.join(root, pathtest)))
print(os.path.join(root, file))
Here is a better code for what you want. All "a.txt" now becomes "b.txt"
import os
rootdir = 'C:/Users/sid/Desktop/test'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
if file == "a.txt"
os.rename(os.path.join(subdir, file),os.path.join(subdir, "b.txt"))

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

shutil.make_archive not zipping to correct destination

As per the code below I am having issues with the zipping a directory using the python 3 shutil.make_archive function. The .testdir will be zipped but it is being zipped in /home/pi, instead of /home/pi/Backups.
zip_loc = '/home/pi/.testdir'
zip_dest = '/home/pi/Backups/'
shutil.make_archive(zip_loc, 'zip', zip_dest)
Could anyone explain what I am doing wrong?
Reading the docs here I came up with:
zip_loc = '/home/pi/.testdir'
zip_dest = '/home/pi/Backups/'
shutil.make_archive(base_dir=zip_loc, root_dir=zip_loc, format='zip', base_name=zip_dest)
From the docs:
base_name is the name of the file to create, including the path, minus any format-specific extension.
root_dir is a directory that will be the root directory of the archive; for example, we typically chdir into root_dir before creating the archive.
base_dir is the directory where we start archiving from; i.e. base_dir will be the common prefix of all files and directories in the archive.
root_dir and base_dir both default to the current directory.
Before to write the archive, move to the good directory :
old_path = os.getcwd()
os.chdir(path)
-> write the archive
After writing the archive move back to old directory :
os.chdir(old_path)

Multitasking in Python, Reformat and Move

I have a many folders. I want to make .zip format of files and move them to another directory:
for (dir, dirs, files) in os.walk(directory):
for filename in files:
# make zip files
if filename.endswith(".zip"):
# move created zip files
os.rename(dir + '/' + filename, new + '/' + directory + '/' + filename)
The problem is that, after make the zip file, program couldn't find the created file with zip extension. I want to create a zip format and move it to another folder.
You can use the shutil lib:
import shutil
shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
shutil.move(src, dst)
Recursively move a file or directory (src) to another location (dst).
If the destination is an existing directory, then src is moved inside
that directory. If the destination already exists but is not a
directory, it may be overwritten depending on os.rename() semantics.
If the destination is on the current filesystem, then os.rename() is
used. Otherwise, src is copied (using shutil.copy2()) to dst and then
removed.
Also you can use shutil for archive file:
shutil.make_archive(base_name, format[, root_dir[, base_dir[,
verbose[, dry_run[, owner[, group[, logger]]]]]]])
Create an archive
file (such as zip or tar) and return its name.
For more info read shutil documentation

node-archiver destination folder

I'm using node-archiver to zip some files and in the zip file I find the archived files under the directory where was zipped from.
So in the zip file, 'myfile.txt' is under this sort of directory:
'home/ubuntu/some_folder/another_folder/'
I want to have the files in the root directory of the zip file.
This is the code I suspect that is in charge with this:
archive.bulk(
{src: filePaths}
);
Its documentation is here although I don't see how to do it.
Any ideas?
Just add expand:true, flatten:true and indicate what name you want to use for destination folder (dest: 'destinationfoldername/')
archive.bulk(
{src: filePaths, dest:'destinationfoldername/', expand:true, flatten:true}
);
It workerd for me

Resources