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
Related
I have problems solving a task. Below you can see my code. I want to work with sub-sub-folders too but my code only moves the sub-folders. How can I do this recursively that it moves all folders in main folder?
path = (r"C:\Users\Desktop\testfolder")
os.chdir(path)
all_files = list(os.listdir())
outputs = os.getcwd()
for files in all_files:
try:
inputs = glob.glob(files + "\\*")
for a in inputs:
shutil.move(a, outputs)
except:
pass
for files in os.listdir("."):
dir_folder = files[:32]
if not os.path.isdir(dir_folder):
try:
os.mkdir(dir_folder)
except:
pass
Here is the new code, that will maintain the folder structure in the output folder as it was in the input folder
from pathlib import Path
import shutil
import os
for path, dir_folder, files in os.walk('./test_folder'):
for file in files:
full_path = os.path.join(path,file)
## Create the path to the subfolder
sub_folder_path = '/'.join(path.split('/')[2:])
# Create output path by joining subfolder path and output directory
output_path = os.path.join(outputs, sub_folder_path)
# Create the output path directory structure if it does not exists, ignore if it does
Path(output_path).mkdir(parents=True, exist_ok=True)
# Move file
shutil.move(full_path, output_path)
You can use os.walk to recursively visit each folder and sub folder and then move all the files in that folder.
import shutil
import os
for path, dir_folder, files in os.walk(input_folder):
for file in files:
full_path = os.path.join(path,file)
move_path = os.path.join(output_path,file)
print(move_path)
shutil.move(full_path, move_path)
print(file)
Where output_path is the path to the folder you move to move the files to.
This will not create the same folder structure in output folder, just move all the files there, do you also want to maintain structure in output folder? If so, I can edit my answer to reflect that
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 have some code to export all files within a zipfile to a path but what I want to do is create a new folder with the same name as the zipfile minus the ".zip" just like the windows explorer option does. I have commented out the code that doesn't work. It seems to be the os.makedirs that doesn't work.
File "C:/Users/brentond/Documents/Python/Unzip all zip files in path.py", line 12
Output = os.path.join(path, filename.replace(".zip", "")) # get new folder path name
^
SyntaxError: invalid syntax
the code:
import os, zipfile
# Define path of zip files to variable
path = r'C:\Users\brentond\Documents\TA2\HA GDMS'
for foldername, subfolders, filenames in os.walk(path): # walk directory
for filename in filenames: # loop through files
if filename.endswith(".zip"): # find zip files
filepath = os.path.join(foldername, filename) # get zip file abs path
#os.makedirs(os.path.join(path, filename.replace(".zip", "")) # create new folder same name as zip file
#Output = os.path.join(path, filename.replace(".zip", "")) # get new folder path name
ZipRef = zipfile.ZipFile(filepath) # create zip file object
ZipRef.extractall(path) # extract all. This to put everything in the path folder
#ZipRef.extractall(Output) # This to put the zip file contents into a folder with same name
ZipRef.close() # close zip
I have resolved this now and simplified the code a little
import os, zipfile
# Define path of zip files to variable
path = r'C:\Users\brentond\Documents\TA2\HA GDMS'
for foldername, subfolders, filenames in os.walk(path): # walk directory
for filename in filenames: # loop through files
if filename.endswith(".zip"): # find zip files
filepath = os.path.join(foldername, filename) # get zip file abs path
filefolder = filename.replace(".zip","")
os.makedirs(os.path.join(path, filefolder)) # create new folder same name as zip file
Output = os.path.join(path, filefolder) # get new folder path name
ZipRef = zipfile.ZipFile(filepath) # create zip file object
#ZipRef.extractall(path) # extract all. This to put everything in the path folder
ZipRef.extractall(Output) # This to put the zip file contents into a folder with same name
ZipRef.close() # close zip
I am working with Python3 and am trying to change the names of files in multiple subdirectories to match the folder name. My directory looks like this:
path: C:\Users\Me\Project
In the path, I have the following folders: alldata, folderA, folderB, folderC
FolderA, folderB, and folderC, each contain a file called data.csv
I want to add the letter name of the folder (e.g., A, B, C) to the file (e.g., dataA.csv) inside the folder and then move all these renamed files to "alldata"
I really appreciate the help!
This one might be a little hardcoded, but is probably more understandable for people who are just starting out in Python:
import os
import shutil
# Enter the 'Project' folder
os.chdir('C:\\Users\\Me\\Project')
# Filter for folders that contain the word 'folder'
folders = [folder for folder in os.listdir() if 'folder' in folder]
for folder in folders:
# Get the last letter of every folder
suffix = folder[-1]
# Build source and destination path for the csv files
source = folder + '\\data.csv'
dest = 'alldata\\data' + suffix + '.csv'
shutil.move(source, dest)
The 'os' module in python gives you access to functions that deal with folders and files. For example, there are functions in the os module to move, copy, rename, delete folders and files. Try this for example:
import os
basePath = "C:\\Users\\Me\\Project\\"
# Rename and move the data.csv file in folderA to dataA.csv in the alldata folder
os.rename(basePath + "folderA\\data.csv", basePath + "alldata\\dataA.csv")
# Rename and move the data.csv file in folderB to dataB.csv in the alldata folder
os.rename(basePath + "folderB\\data.csv", basePath + "alldata\\dataB.csv")
# Rename and move the data.csv file in folderC to dataC.csv in the alldata folder
os.rename(basePath + "folderC\\data.csv", basePath + "alldata\\dataC.csv")
# Make sure that they moved as intended
filesInAllDataFolder = os.listdir(basePath + "alldata\\")
print(filesInAllDataFolder)
The os module is super handy and I guarantee you'll use it a lot, so play with it!
This works for me:
import os
def scan_dir(folder):
for name in os.listdir(folder):
path = os.path.join(folder, name)
if os.path.isfile(path):
if 'data' in path:
dir_name = path.split('/')[-2]
new_name_path = path[:-3]+dir_name+'.csv'
new_name_path = new_name_path.split('/')
new_name_path[-2] = 'alldata'
new_name_path = "/".join(new_name_path)
os.rename(path, new_name_path)
else:
scan_dir(path)
directory = 'C:\Users\Me\Project'
scan_dir(directory)
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:]