I need to make a tar from files in the directory
Here is my code
import tarfile
import os
path = '/home/atom/Desktop/test_arch/sample.tar'
source = '/home/atom/Desktop/test_arch'
files = os.listdir(source)
files.sort()
tar = tarfile.open(path, "w")
for file in files:
print(file)
file_path = source + '/' + file
tar.add(file_path)
tar.close()
Everything works fine. The archive is created. But instead of a list of files in it. I got several subsolders:
/home/atom/Desktop/test_arch
And only in the last subfolder are my files
If I try:
tar.add(file)
It gives an Error:
FileNotFoundError: [Errno 2] No such file or directory: '1.jpg'
You should change Current Working Directory to work directly in directory source
and then you can use filename without source/ and archive will keep it without source/.
import tarfile
import os
source = '/home/atom/Desktop/test_arch'
path = os.path.join(source, 'sample.tar')
# --- keep current directory and go to source directory
old_dir = os.getcwd()
os.chdir(source) # change directory
# --- compress using only names (or relative paths)
files = sorted(os.listdir())
tar = tarfile.open(path, "w")
for filename in files:
print(filename)
if filename != 'sample.tar':
tar.add(filename)
tar.close()
# --- go back to previuos directory
os.chdir(old_dir)
Related
Usually I navigate to the folder I am extracting data from and copy the file name directly:
df2=pd.read_csv('10_90_bnOH-MEA.csv',usecols=[1])
If I have multiple files and want to do the same for all the files, how do I specify the folder to open and get all the files inside?
I want to run the above code without specifying the file's full path
(C:\Users\X\Desktop\Y\Z\10_90_bnOH-MEA.csv)
You want listdir from the os module.
import os
path = "C:\\Users\\X\\Desktop\\Y\\Z\\"
files = os.listdir(path)
print(files)
dataframe_list = []
for filename in files:
dataframe_list.append(pd.read_csv(os.path.join(path,filename)))
You should open the desired directory and loop through all the files then do something to them.
# import required module
import os
# assign directory
directory = 'files'
# iterate over files in
def goThroughDirectory(directory):
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
# checking if it is a file
if os.path.isfile(f):
# do something
If you also want to loop through all the files in a directory you should add a check for if os.path.isdir(f) like this
...
def goThroughDirectory(directory):
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
# checking if it is a file
if os.path.isfile(f):
# do something
elif os.path.isdir(f):
# its not a file but a directory then loop through that directory aswell
goThroughDirectory(directory + "\" + f)
for more information you should check geeksforgeeks
I'm trying to make this code which take the content of each folder in directory and add it to zip one by one with the name of folder
I did made this code but I'm blocked with just add file by extension in zip
import zipfile, os
handle = zipfile.ZipFile('ALL-PY.zip', 'w')
for x in os.listdir():
if x.endswith(directory):
handle.write(x,compress_type = zipfile.ZIP_DEFLATED)
handle.close()
I would follow this approach:
import zipfile, os
handle = zipfile.ZipFile('ALL-PY.zip', 'w')
path = "C:/Users/User_Name/my_directory" # This is YOUR INPUT - set with the directory you want to zip
os.chdir(path)
for directory, subs, files in os.walk("."):
handle.write(directory)
for this_file in files:
handle.write(os.path.join(directory, this_file), compress_type = zipfile.ZIP_DEFLATED)
handle.close()
I have several folders that have a naming convention of "monthly_vendor_report_####", where #### is just a random combination of numbers. Each folder has a CSV file and I'd like to move the CSVs files out of the folder to a new destination source. So far this is what I have, which only unzips the files:
import os, zipfile
dir_name = r"C:\Users\...."
extension = ".zip"
os.chdir(dir_name) # change directory from working dir to dir with files
for item in os.listdir(dir_name): # loop through items in dir
if item.endswith(extension): # check for ".zip" extension
file_name = os.path.abspath(item) # get full path of files
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
zip_ref.extractall(dir_name) # extract file to dir
zip_ref.close() # close file
os.remove(file_name) # delete zipped file
Screenshot of folders--each folder contains a CSV
Content of one of the folders
You can use shutil to copy or move the files.
#Note that all the CSV files are taking the same names as their folders
import os
import shutil
# Open a file
path = 'C:\foo'
os.chdir(path)
dirs = filter(os.path.isdir, os.listdir(path))
destination= 'C:\Output'
# This would copy all listed CSV files
for dir in dirs:
file= dir + ".csv"
src= path + "\\" + dir + "\\" + file
shutil.copy(src, destination)
I am wanting to add explicit entries for the directories in my zipfile. For example my zipfile includes:
assets/images/logo.png
When in reality I need it to include:
assets/
assets/images/
assets/images/logo.png
So my question is, how do I add the directories as explicit entries using relative paths? I tried to use zip.write(relative/path/to/directory) but it says it can't find the directory, since its a relative path. It works when I put
/Users/i510118/Desktop/Engineering/kit-dev-portal-models/src/kit_devportal_models/static_data/static_extension/created_extnsn_version_update2/assets/images/logo.png
inside of zipfile.write(), but I need it to just be the relative path, which is just
assets/images/logo.png
is this possible?
Here is my full code
buf = io.BytesIO()
zipObj = zipfile.ZipFile(buf, "w")
extension_folder = "created_extnsn_version_update2"
with zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(path_to_extension_directory):
# If the folder is not the root folder the extension is in
if not folderName.endswith(extension_folder):
folder = folderName.split(f"{extension_folder}/", 1)[1]
else:
folder = ''
for filename in filenames:
# create complete filepath of file in directory
filePath = os.path.relpath(os.path.join(folderName, filename), path_to_extension_directory)
with open(f"{folderName}/{filename}", 'rb') as file_data:
bytes_content = file_data.read()
# Add folder to zip if its not the root directory
if folder:
zipObj.write(folder)
# Add file to zip
zipObj.writestr(filePath, bytes_content)
# edit zip file to have all permissions
zf = zipfile.ZipFile(buf, mode='a')
info = zipfile.ZipInfo(f"{folderName}/{filename}")
info.external_attr = 0o777 << 16
zf.writestr(info, f"{folderName}/{filename}")
# Rewind the buffer's file pointer (may not be necessary)
buf.seek(0)
return buf.read()
Please let me know if you need more information!
From code stolen from add empty directory using zipfile?
import zipfile
zf = zipfile.ZipFile('test.zip', 'w')
folders = [
"assets/",
"assets/images/",
]
for n in folders:
zfi = zipfile.ZipInfo(n)
zf.writestr(zfi, '')
zf.write('logo.png', 'assets/images/logo.png')
zf.close()
zf = zipfile.ZipFile('test.zip', 'r')
for i in zf.infolist():
print(f"is_dir: {i.is_dir()}; filename: {i.filename}")
zf.close()
How can I extract all the .zip files in a certain directory to its parent directory?
I tried:
import zipfile
parent_directory = '../input'
directory = '../input/zip'
for f in os.listdir(directory):
with zipfile.ZipFile(os.path.join(directory,f), "r") as z:
z.extractall(parent_directory)
However the unzipped files are not saved in '..input/zip', they are saved in nested folders
This might be a bit exaggerated.
After files are unzipped, I run this to:
move the original .zip file up one directory level. (to avoid /src_filename' already exists error)
move all files from all subdirectories into the zip parent directory.
move the original .zip file back into the parent directory.
import os
import shutil
src = r'C:\Users\Owner\Desktop\PythonZip\PyUnzip01\child_dir\unzip_test2'
dest = r'C:\Users\Owner\Desktop\PythonZip\PyUnzip01\child_dir'
pdir = '../PyUnzip01'
os.replace(r"C:\Users\Owner\Desktop\PythonZip\PyUnzip01\child_dir\unzip_test2.zip", r"C:\Users\Owner\Desktop\PythonZip\PyUnzip01\unzip_test2.zip")
for root, subdirs, files in os.walk(src):
for file in files:
path = os.path.join(root, file)
shutil.move(path, dest)
os.replace(r"C:\Users\Owner\Desktop\PythonZip\PyUnzip01\unzip_test2.zip", r"C:\Users\Owner\Desktop\PythonZip\PyUnzip01\child_dir\unzip_test2.zip")