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)
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 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 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)
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 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