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
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 work as an IT help desk associate and have to constantly clear out disk space for VDIs. I want to write a code that can clear out specific folders all at once to save time. I wrote a code, but only have it to run one folder at a time. Is there a way for me to make it a function and just make variables for each path and have them all cleared at once. Sample:
import os
#path to delete
path = r"C:\Users\jwals\test test"
#will check if the path is a dir or not
if os.path.exists(path):
#iterating through subfolders
for root_folder, folders, files in os.walk(path):
#checking for files
for file in files:
#file path
file_path = os.path.join(root_folder, file)
#delete the files in that path
if not os.remove(file_path):
#will print if successful
print(f"{file_path} deleted successfully")
#unsuccessful message (if this prints something went wrong)
else:
print(f"Unable to delete the {file_path}")
IIUIC, you can simply do this:
import os
#paths to delete
paths = [r"C:\Users\jwals\test test", r"C:\Users\jwals\another test"]
def delete_files(path):
#will check if the path is a dir or not
if os.path.exists(path):
#iterating through subfolders
for root_folder, folders, files in os.walk(path):
#checking for files
for file in files:
#file path
file_path = os.path.join(root_folder, file)
...
for path in paths:
delete_files(path)
I am trying to move all files that are in Just source_path to the target_path.
Here is the code:
import os
target_path = 'C:/Users/Justi/source/repos/GUI Developmentv3/Test 02' + '\\'
source_path ='C:/Users/Justi/source/repos/GUI Developmentv3/' + '\\'
for path, dir, files in os.walk(source_path):
if files:
for file in files:
if not os.path.isfile(target_path + file):
os.rename(path + '\\' + file, target_path + file)
However, this also moves other files that are in the sub-directory folders of the source folder into the target folder.
May I check how I could change the code to only move files at the exact directory of source_path without affecting files in source_path's sub-dictionary? Thank you in advance.
I think the problem is the usage of os.walk, because it walks the whole tree. See the docs:
Generate the file names in a directory tree by walking the tree either top-down or bottom-up.
You are only interested in the contents of a particular folder, so os.listdir is more suitable:
Return a list containing the names of the entries in the directory given by path.
The following code will iterate over the files in the source dir, and move only the files into the target dir.
import os
import shutil
source = "source"
target = "target"
for f in os.listdir(source):
print(f"Found {f}")
if os.path.isfile(os.path.join(source, f)):
print(f"{f} is a file, will move it to target folder")
shutil.move(os.path.join(source, f), os.path.join(target, f))
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
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")