How to have ZipFile only zip a specified directory - Python 3 - python-3.x

When I try to zip a directory with the following code, my directory is zipped and contains all the files I would like zipped, however it is also zipping the root directories for the directory I would like zipped.
(Test is the target directory to be zipped - it contains other directories and files) When unzipping the my_python_files.zip, it unzips with absolute paths:
unzipping my_python_files.zip:
\Users\hhafez\Desktop\Test
when I would like to have:
\Test
I am having trouble trying to find a way to avoid this, any tips would be much appreciated.
def get_all_file_paths(directory):
file_paths = []
for root, directories, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
file_paths.append(filepath)
return file_paths
def zipfiles():
file_paths = get_all_file_paths(r"C:\Users\hhafez\Desktop\Test")
with ZipFile('my_python_files.zip','w') as myzip:
for file in file_paths:
print(file)
myzip.write(file)
print('All files zipped successfully!')
zipfiles()

if you want to zip the list of files and dont want the zip archive to contain the absolute paths of each file...
def zipFiles(directory):
parentDir = "C:\Users\hhafez\Desktop" #hardcoding parent path to strip
with ZipFile('my_python_files.zip','w') as myzip:
for root, directories, files in os.walk(directory):
zipFileName = root[len(parentDir):] #always take whats after the parentDir for the filename going in the zip
for file in files:
myzip.write(os.path.join(root,file), os.path.join(zipFileName,file), compress_type=zipfile.ZIP_DEFLATED)
zipfiles(r"C:\Users\hhafez\Desktop\Test")
This should accomplish what you need. The major difference here is the zipFileName variable.
zipFileName = root[len(parentDir):]
This line strips out the parentDir from the directory that you are crawling through . zipFileName and the name of the file would be the archive name to pass to myzip.write which explains this:
myzip.write(os.path.join(root,file), os.path.join(zipFileName,file), compress_type=zipfile.ZIP_DEFLATED)

Related

How do I preserve directories when zipping up a file in python?

I have the following function where I convert a directory to bytes, but the directories inside the parent directory are not being preserved. How do I preserve the directories? Here's what I have tried:
buf = io.BytesIO()
zipObj = ZipFile(buf, "w")
with zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(path_to_extension_directory):
for filename in filenames:
# create complete filepath of file in directory
filePath = os.path.join(folderName, filename)
with open(f"{folderName}/{filename}", 'rb') as file_data:
bytes_content = file_data.read()
# Add file to zip
zipObj.writestr(filePath, bytes_content)
# Rewind the buffer's file pointer (may not be necessary)
buf.seek(0)
return buf.read()
This returns the bytes of the files but when I open it, all of the files are in the same directory. I thought that adding the filePath as the first parameter in writestr would do it, but it does not. Thanks for your help! Please let me know if I can provide any additional information.
Try replacing
filePath = os.path.join(folderName, filename)
with:
filePath = os.path.relpath(os.path.join(folderName, filename), path_to_extension_directory)

Loop through folders and create new subfolders for each then move images in Python

Let's say I have a folder which has subfolders project1, project2, project3, ... in it.
In each of project, I have subfolders fixedly named process, progress and session in it, inside those subfolders, there are also other subfolders and image files.
Now I want to create subfolders files1 for each project to move all the images from process, and create files2 to move all the images from progress and session.
Please note the names of images for each projects are unique, so we ignore image name duplicates issues.
For creating files1 for project1, I use:
import os
dirs = './project1/files1'
if not os.path.exists(dirs):
os.makedirs(dirs)
But I need to loop through all the projects folders.
How could we do that in Python? Sincere thanks.
For create file1 and file2 for each project:
# Remove non-images files
base_dir = './'
for root, dirs, files in os.walk(base_dir):
for file in files:
# print(file)
pic_path = os.path.join(root, file)
ext = os.path.splitext(pic_path)[1].lower()
if ext not in ['.jpg', '.png', '.jpeg']:
os.remove(pic_path)
print(pic_path)
# create files1 and files2
for child in os.listdir(base_dir):
child_path = os.path.join(base_dir, child)
os.makedirs(child_path + '/file1', exist_ok=True)
os.makedirs(child_path + '/file2', exist_ok=True)

extracting file content inside multiple folders and printing required folder names

I have files and directory structure like this:
C:\data\4512\required_data\121\a.txt
C:\data\4512\required_data\121\b.txt
C:\data\4512\required_data\456\c.txt
C:\data\4512\required_data\456\d.txt
C:\data\9813\required_data\789\e.txt
C:\data\9813\required_data\789\f.txt
C:\data\9813\required_data\301\g.txt
C:\data\9813\required_data\301\h.txt
I want to print the content of the text files
Also I want to print outer_folder number like 4512, inner folder number like 121 and the file name
I was trying some code like this:
path = "C:\\data"
for root, dirs, files in os.walk(path):
for dir in dirs:
print(dir)
data_location = os.path.join(path, dir, "required_data")
for example, for this case:
C:\data\4512\required_data\121\a.txt
Expected output:
file=open("a.txt")
print(file) # content of file
print("outer_number") # 4512
print("inner_number") # 121
print("name_of_file") # a.txt
I want to do this for all files

creating corresponding subfolders and writing a portion of the file in new files inside those subfolders using python

I have a folder named "data". It contains subfolders "data_1", "data_2", and "data_3". These subfolders contain some text files. I want to parse through all these subfolders and generate corresponding subfolders with the same name, inside another folder named "processed_data". I want to also generate corresponding files with "processed" as a prefix in the name and want to write all those lines from the original file where "1293" is there in the original files.
I am using the below code but not able to get the required result. Neither the subfolders "data_1", "data_2", and "data_3" nor the files are getting created
import os
folder_name=""
def pre_processor():
data_location="D:\data" # folder containing all the data
for root, dirs, files in os.walk(data_location):
for dir in dirs:
#folder_name=""
folder_name=dir
for filename in files:
with open(os.path.join(root, filename),encoding="utf8",mode="r") as f:
processed_file_name = 'D:\\processed_data\\'+folder_name+'\\'+'processed'+filename
processed_file = open(processed_file_name,"w", encoding="utf8")
for line_number, line in enumerate(f, 1):
if "1293" in line:
processed_file.write(str(line))
processed_file.close()
pre_processor()
You might need to elaborate on the issue you are having; e.g., are the files being created, but empty?
A few things I notice:
1) Your indentation is off (not sure if this is just a copy-paste issue though): the pre_processor function is empty, i.e. you are defining the function at the same level as the declaration, not inside of it.
try this:
import os
folder_name=""
def pre_processor():
data_location="D:\data" # folder containing all the data
for root, dirs, files in os.walk(data_location):
for dir in dirs:
#folder_name=""
folder_name=dir
for filename in files:
with open(os.path.join(root, filename), encoding="utf8",mode="r") as f:
processed_file_name = 'D:\\processed_data\\'+folder_name+'\\'+'processed'+filename
processed_file = open(processed_file_name,"w", encoding="utf8")
for line_number, line in enumerate(f, 1):
if "1293" in line:
processed_file.write(str(line))
processed_file.close()
pre_processor()
2) Check if the processed_data and sub_folders exist; if not, create them first as this will not do so.
Instead of creating the path to the new Folder by hand you could just replace the name of the folder.
Furthermore, you are not creating the subfolders.
This code should work but replace the Linux folder slashes:
import os
folder_name=""
def pre_processor():
data_location="data" # folder containing all the data
for root, dirs, files in os.walk(data_location):
for dir in dirs:
# folder_name=""
folder_name = dir
for filename in files:
joined_path = os.path.join(root, filename)
with open(joined_path, encoding="utf8", mode="r") as f:
processed_folder_name = root.replace("data/", 'processed_data/')
processed_file_name = processed_folder_name+'/processed'+filename
if not os.path.exists(processed_folder_name):
os.makedirs(processed_folder_name)
processed_file = open(processed_file_name, "w", encoding="utf8")
for line in f:
if "1293" in line:
processed_file.write(str(line))
processed_file.close()
pre_processor()

How to use os.walk and use intersection files in two folders and sub subfolders (python)

I'm trying to compare two folders all the files , files with same file name , and many files in subfolders , and print full path
def aaa(self):
dira = self.lineA.text()
dirb = self.lineB.text()
dirafile = [os.path.join(path, dirafile) for path, dirs, files in os.walk(dira) for dirafile in files]
dirbfile = [os.path.join(path, dirbfile) for path, dirs, files in os.walk(dirb) for dirbfile in files]
dirfileok = set(dirbfile).intersection(dirafile)
print(dirfileok)
but print results "set()" , Why ???

Resources