I am trying to archive the oldest date modified folder and move to another folder.
My folder structure is currently c:\test\test1 (latest folder) and c:\test\test2 and c:\test\test3
I would like to zip test2 and test 3 as they are oldest modified folders and move them to another location i.e c:\archive
I have managed to get it working to zip the oldest folder but it does not move it and also does not move to another folder location which I would like it to keep my archive tidy.
Also a nice to have would be to have the zipfolder with archive_date.zip
import glob
import os
import zipfile
files_dir = r'c:\test\' # here should be path to directory with your files
files = glob.glob(files_dir + '\*')
# find all files that located in specified directory
files_modify_dt = [os.path.getmtime(file) for file in files]
# take files except last modified file
files_to_zip = [file for _, file in sorted(zip(files_modify_dt, files))][:-1]
# zip of selected files
with zipfile.ZipFile(os.path.join(files_dir, 'archive.zip'), 'w', zipfile.ZIP_DEFLATED) as zip_obj:
for file in files_to_zip:
zip_obj.write(file, os.path.basename(file))
os.remove(file)
Related
Can anyone help me to convert a lot of zip files at once using python?
I have "Years" folder which has 3 folders of "2019","2020" and "2021". Folder "2019" has folders of "1","2","3" folders. Folder "1" has "attachment.zip", Folder "2" has "attachment.zip" and Folder "3" has "attachment.zip. I want to go through each folder in order to convert all zip files and extract them to "Extracted" folder.
Result should look like this:
Folder "1" has "attachment.zip" and folder of "Extracted".
Below is the code that worked for me:
import os, zipfile
dir_name = 'C:\\SomeDirectory'
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
Looking back at the code I had amended, the directory was getting
confused with the directory of the script.
The following also works while not ruining the working directory.
First remove the line
os.chdir(dir_name) # change directory from working dir to dir with files
Then assign file_name as
file_name = dir_name + "/" + item
I am trying to archive existing file apart from the latest modified file in Python or FME. I have managed to get it to point where I can get python pick up the latest modified file but any ideas on how I can archive all the files i have in my folder apart from the last modified file?
Thank You
You can solve your problem using this snippet of code:
import glob
import os
import zipfile
files_dir = r'C:\Users\..\files' # here should be path to directory with your files
files = glob.glob(files_dir + '\*')
# find all files that located in specified directory
files_modify_dt = [os.path.getmtime(file) for file in files]
# take files except last modified file
files_to_zip = [file for _, file in sorted(zip(files_modify_dt, files))][:-1]
# zip of selected files
with zipfile.ZipFile(os.path.join(files_dir, 'archive.zip'), 'w', zipfile.ZIP_DEFLATED) as zip_obj:
for file in files_to_zip:
zip_obj.write(file, os.path.basename(file))
os.remove(file)
One main folder that has one folder named old and another called new
The old has some folders. The new has a few of these folders with same names and nothing more.
I want to delete the folders of the old that are not present in the new first and then: make a loop that will take each file -same name -pair, and put it in the
the following line:
arcpy.Append_management(["shpfromonefolder.shp", "shpfromsecondfolder.shp"],"NO_TEST")
for example: land.shp from one folder with land.shp from the other folder so it will be:
arcpy.Append_management(["land.shp", "land.shp"],"NO_TEST")
This will delete folders in old_path if they exist do not in new_path:
import os
import shutil
old_path = r"old file path"
new_path = r"old file path"
for folder in os.listdir(old_path):
if folder not in os.listdir(new_path):
shutil.rmtree(os.path.join(old_path, folder))
This will find the matching shape files and pass them to arcpy.Append_management():
import os
import arcpy
for dir_path, dir_names, file_names in arcpy.da.Walk(workspace=new_path, datatype="FeatureClass"):
for filename in file_names:
new_file_path = os.path.join(dir_path, filename)
folder = os.path.basename(os.path.dirname(new_file_path))
old_file_path = os.path.join(old_path, folder, filename)
if os.path.exists(old_file_path):
arcpy.Append_management(inputs=[new_file_path], target=old_file_path, schema_type="NO_TEST")
I have written a script. It finds the current path and changes the path and zips. Then I want that it just find the zip file copy it to another directory and at the end removes the content of the folder. But it zips once and zips again the whole folders and zip-file. The intial situation is as in Figure 1.
The script is like this:
import os
import zipfile
import shutil
import glob
Pfad = os.getcwd()
newPfad = 'D'+ Pfad[1:]
Zip_name=os.path.basename(os.path.normpath(Pfad))
shutil.make_archive(Zip_name, 'zip', Pfad)
if not os.path.exists(newPfad):
os.makedirs(newPfad)
dest_dir=newPfad
files = glob.iglob(os.path.join(Pfad, "*.zip"))
for file in files:
if os.path.isfile(file):
shutil.copy2(file, dest_dir)
shutil.rmtree(Pfad)
And finally the result is illustrated in the following figure.
The batch file is just for running the python script.
How can I get the following desired situation?
The issue is that zip file is created prior to listing the directory contents, therefore empty zip file is added to. Create archive in the parent directory and then move it. Moving a file or directory is cheap and atomic.
import os
import shutil
cwd = os.path.abspath(os.path.curdir)
zip_target = os.path.join(cwd, os.path.basename(cwd)) + '.zip'
zip_source = shutil.make_archive(cwd, 'zip')
os.rename(zip_source, zip_target)
Please help I am new in python. I have multiple files from different subfolder of a root directory with .wmv extension replaced with .mp4 and create a folder name base on the filename by removing other character starting at '#'. How can I insert the string 'DATA_' at the start of a folder name. All new created folders should starts with "DATA_". MP4 files will be moved in a new created folder name.
example:
Directory: D:\GE-J1N-2991
Files found located at:
D:\GE-J1N-2991\1-ZWC4\20160705051835547#DXV_Ch1.wmv
D:\GE-J1N-2991\2-QWD4\20160705051836647#DXV_Ch1.wmv
D:\GE-J1N-2991\2-QWD4\34-QWERD\20160705084433078#DXV_Ch1.wmv
New created folder:
D:\GE-J1N-2991\1-ZWC4\20160705051835547\
D:\GE-J1N-2991\2-QWD4\20160705051836647\
D:\GE-J1N-2991\2-QWD4\34-QWERD\20160705084433078\
It should be renamed like this:
D:\GE-J1N-2991\1-ZWC4\DATA_20160705051835547\20160705051835547#DXV_Ch1.mp4
D:\GE-J1N-2991\2-QWD4\DATA_20160705051836647\20160705051836647#DXV_Ch1.mp4
D:\GE-J1N-2991\2-QWD4\34-QWERD\DATA_20160705084433078\20160705084433078#DXV_Ch1.mp4
Code:
import os, glob, shutil, fnmatch
folder = 'D:\\GE-J1N-2991'
os.chdir(folder)
def locate_WMV(pattern, root=os.curdir):
for path, dir, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
for asf in locate_WMV("*.wmv"):
os.rename(asf, asf[:-4] + ".mp4")
for f in locate_WMV("*#DVX_Ch1.mp4"):
new_dir = f.rsplit('#', 1)[0]
os.mkdir(new_dir)
shutil.move(f, os.path.join(new_dir, os.path.basename(f)))
If i use this code, the folder is renamed but created in Directory: D:\GE-J1N-2991 which is wrong, it should be created in where the file was found
new_dir2 = 'DATA_'+ f.rsplit('#', 1)[0].strip()[20:]