I started learn python and trying to create 'backup' app. Want to add files from chosen directory in zip archive, but I don't understand why zipfile.write adding the same files from directory in arhive non-stop? Also it add itself to archive.
import zipfile, os, pathlib, time
from os.path import basename
now = time.strftime('%H%M%S')
source3 = 'F:\oneMoreTry'
# create a ZipFile object
with zipfile.ZipFile(now + '.zip', 'w') as zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(source3):
for filename in filenames:
# create complete filepath of file in directory
filePath = os.path.join(folderName, filename)
# Add file to zip
zipObj.write(filePath, basename(filePath))
Related
import os
from zipfile import ZipFile
from os.path import basename
src = "C:\git\mytest"
full_path=[]
for root, dirs, files in os.walk(src, topdown=False):
for name in files:
full_path.append(os.path.join(root, name))
with ZipFile('output.zip', 'w') as zipObj:
for item in full_path:
zipObj.write(item, basename(item))
Trying to create a zip file with containing some file of a specific folder.
In specific folder has some files. then it will add to zip file
In the mentioned code, one zipfile is created but there is no file. I am not getting the exact reason
I work in audio and I need a number of files transcribed by a third party. To do so I have to swap out an entire directory of .wav files with .mp3s I have compressed while still maintaining the file directory. It's about 20,000 files.
e.g.
wav:
Folder1
Folder 1a
sound1.wav
sound2.wav
Folder 1b
sound3.wav
sound4.wav
Folder2
Folder 2a
Folder 2aa
sound5.wav
sound6.wav
Folder 2ab
sound7.wav
Folder2b
sound8.wav
etc.
mp3:
Folder1
sound1.mp3
sound2.mp3
sound3.mp3
sound4.mp3
sound5.mp3
sound6.mp3
sound7.mp3
sound8.mp3
etc.
I had to group them together to do the batch compression in Adobe Audition, but now I would like to be able to switch them out with the wav files that are perfectly identical save for file extension as doing this manually is not a reasonable option.
Any help would be greatly appreciated. I have a little experience with python so that language is preferable, but I'm open to any solutions.
You can use a combination of glob and shutil to do this. Try running this script from inside Folder1.
from glob import glob
from shutil import move
import os
wav_files = glob('**/*.wav', recursive=True)
for wf in wav_files:
file_path = os.path.splitext(wf)[0]
file_head = os.path.split(file_path)[-1]
try:
move('./{}.mp3'.format(file_head),
'{}.mp3'.format(file_path))
except:
print('Could not find or move file {}.mp3, it may not exist.'.format(file_head))
What I understand is that you want the same directory structure for mp3 as for vaw.
You can:
browse the directory structure of vaw file and construct a mapping between base names (file names without extension) and relative path.
browse the directory structure, searching the mp3 files and find each relative path in the mapping, creating the target directory structure if missing and move the file in.
For instance:
import os
vaw_dir = 'path/to/MyVaw' # parent of Folder1...
musics = {}
for root, dirnames, filenames in os.walk(vaw_dir):
for filename in filenames:
basename, ext = os.path.splitext(filename)
if ext.lower() == '.wav':
relpath = os.path.relpath(root, vaw_dir)
print('indexing "{0}" to "{1}"...'.format(filename, relpath))
musics[basename] = relpath
else:
print('skiping "{0}"...'.format(filename))
mp3_dir = 'path/to/MyMp3'
out_dir = vaw_dir # or somewhere else
for root, dirnames, filenames in os.walk(vaw_dir):
for filename in filenames:
basename, ext = os.path.splitext(filename)
if ext.lower() == '.mp3' and basename in musics:
relpath = musics[basename]
path = os.path.join(out_dir, relpath)
if not os.path.exists(path):
print('creating directory "{0}"...'.format(path))
os.makedirs(path)
src_path = os.path.join(root, filename)
dst_path = os.path.join(path, filename)
if src_path != dst_path:
print('moving "{0}" to "{1}"...'.format(filename, relpath))
os.rename(src_path, dst_path)
else:
print('skiping "{0}"...'.format(filename))
print("Done.")
I'm trying to create a zip file by zipping couple text files from a specific directory. My code looks like the following:
import zipfile,os
project='C:/Users/user1/Documents/work/filesToZip'
dirlist = os.listdir(project)
print dirlist
zip_name = zipfile.ZipFile(os.path.join(project,'jobs.zip'),'w')
for file in dirlist:
zip_name.write(os.path.join(project,file))
zip_name.close()
the code runs fine and it creates the zip file, the only problem is when I open the zip file I found the whole directory structure is zipped. i.e. when I open the file I will find Users open it then user1 open it then Documents open it then work then filesToZip then I find the files I want to zip. my question is how can I get red of the file structure so when I open the zip file I find the files I zipped right away?
Thanks in advance!
ZipFile.write has an optional second parameter archname
which does exactly what you want.
import zipfile,os
project='C:/Users/user1/Documents/work/filesToZip'
# prevent adding zip to itself if the old zip is left in the directory
zip_path = os.path.join(project,'jobs.zip')
if os.path.exists(zip_path):
os.unlink(zip_path);
dirlist = os.listdir(project)
zip_file = zipfile.ZipFile(zip_path, 'w')
for file_name in dirlist:
zip_file.write(os.path.join(project, file_name), file_name)
zip_file.close()
For python 2.7+ you can use shutil instead:
from shutil import make_archive
make_archive(
'zipfile_name',
'zip', # the archive format - or tar, bztar, gztar
root_dir=None, # root for archive - current working dir if None
base_dir=None) # start archiving from here - cwd if None too
This way you can explicitly specify which directory should be the root_dir and which should be the base_dir. If root_dir and base_dir are not the same, it will only zip the files in base_dir but preserve the directory structure up to root_dir
import zipfile,os
project='C:/Users/user1/Documents/work/filesToZip'
original_dir= os.getcwd()
os.chdir(project)
dirlist = os.listdir(".")
print dirlist
zip_name = zipfile.ZipFile('./jobs.zip','w')
for file in dirlist:
zip_name.write('./'+file)
zip_name.close()
os.chdir(original_dir)
I am trying to use the os.walk() module to go through a number of directories and move the contents of each directory into a single "folder" (dir).
In this particular example I have hundreds of .txt files that need to be moved. I tried using shutil.move() and os.rename(), but it did not work.
import os
import shutil
current_wkd = os.getcwd()
print(current_wkd)
# make sure that these directories exist
dir_src = current_wkd
dir_dst = '.../Merged/out'
for root, dir, files in os.walk(top=current_wkd):
for file in files:
if file.endswith(".txt"): #match files that match this extension
print(file)
#need to move files (1.txt, 2.txt, etc) to 'dir_dst'
#tried: shutil.move(file, dir_dst) = error
If there is a way to move all the contents of the directories, I would be interested in how to do that as well.
Your help is much appreciated! Thanks.
Here is the file directory and contents
current_wk == ".../Merged
In current_wkthere is:
Dir1
Dir2
Dir3..
combine.py # python script file to be executed
In each directory there are hundreds of .txtfiles.
Simple path math is required to find source files and destination files precisely.
import os
import shutil
src_dir = os.getcwd()
dst_dir = src_dir + " COMBINED"
for root, _, files in os.walk(current_cwd):
for f in files:
if f.endswith(".txt"):
full_src_path = os.path.join(src_dir, root, f)
full_dst_path = os.path.join(dst_dir, f)
os.rename(full_src_path, full_dst_path)
You have to prepare the complete path of source file, and make sure dir_dst exists.
for root, dir, files in os.walk(top=current_wkd):
for file in files:
if file.endswith(".txt"): #match files that match this extension
shutil.move(os.path.join(root, file), dir_dst)
Im trying to create a zipfile with the content of a folder ( some dirs and files ) using the code belllow:
import zip,os
path = "c:\\tester\\folderToZip"
zip = zipfile.ZipFile("zippedFolder.zip", "w")
for root, dirs, files in os.walk(path):
for file in files:
zip.write(os.path.join(root, file))
zip.close()
But after the code runs, when i check the zip file, the zip file, instead having the content of the folder "folderToZip" ( ex: f1,f2,f2, a.txt,b.txt,c.txt ) it have the full path of the variable path.
So, the question is, how can i create a zipfile based on a folder content, but not his fullpath ?
write takes a second optional parameter arcname, which when specified will provide the name for the file stored. Use that to specify the name you want.
If you only want the filename:
import zip,os
path = "c:\\tester\\folderToZip"
zip = zipfile.ZipFile("zippedFolder.zip", "w")
for root, dirs, files in os.walk(path):
for file in files:
filename = os.path.join(root, file)
zip.write(filename, filename)
zip.close()