How can I delete contents of a folder but keep the folder? - python

I have seen several questions here, but all of them seem to delete the folder as well.
How can I delete only the contents of a particular folder, but keep the folder itself.
Preferably for two conditions:
Contents
Deletes recursively under all all subfolders. But keeps the main folder.

import os
def functToDeleteItems(fullPathToDir):
for itemsInDir in os.listdir(fullPathToDir):
if os.path.isdir(os.path.join(fullPathToDir, itemsInDir)):
functToDeleteItems(os.path.isdir(os.path.join(fullPathToDir, itemsInDir)))
else:
os.remove(os.path.join(fullPathToDir,itemsInDir))
Here function "functToDeleteItems" will take one argument that is "fullPathToDir" which will contain full path of the folder whose content you wan to delete. It will recursively call itself it if find any folder inside it and if found any file then delete it.

Related

How do I copy subfolders into another location

I'm making a program to back up files and folders to a destination.
The problem I'm currently facing is if I have a folder inside a folder and so on, with files in between them, I can't Sync them at the destination.
e.g.:
The source contains folder 1 and file 2. Folder 1 contains folder 2, folder 2 contains folder 3 and files etc...
The backup only contains folder 1 and file 2.
If the backup doesn't exist I simply use: shutil.copytree(path, path_backup), but in the case, I need to sync I can't get the files and folders or at least I'm not seeing a way to do it. I have walked the directory with for path, dir, files in os.walk(directory) and even used what someone suggest in another post:
def walk_folder(target_path, path_backup):
for files in os.scandir(target_path):
if os.path.isfile(files):
file_name = os.path.abspath(files)
print(file_name)
os.makedirs(path_backup)
elif os.path.isdir(files):
walk_folder(files, path_backup)
Is there a way to make the directories in the backup folder from the ground up and then add the info alongside or is the only way to just delete the whole folder and use shutil.copytree(path, path_backup).
With makedirs, all it does is say it can't create because the folder already exists, this is understandable as it's trying to write in the Source folder and not in the backup. Is there a way to make the path to replace Source for backup?
If any more code is needed feel free to ask!

How to run python script for every folder in directory and skip any empty folders?

I am trying to figure out how to generate an excel workbook for each subfolder in my directory while skipping the folders that are empty. My directory structure is below.
So it would start with Folder A, execute my lines of code to create an excel file using Folder A's contents, then move to Folder B, execute my lines of code to create a separate excel file using Folder B's contents, then move to Folder C and skip it since it's empty, and continue on.
How do I loop through each folder in this manner and keep going when a folder is empty?
I would greatly appreciate the help!
myscript.py
folderA
- report1.xlsx
- report2.xlsx
folderB
- report1.xlsx
- report2.xlsx
folderC
** EMPTY **
folderD
- report1.xlsx
- report2.xlsx
Something like this maybe?
from pathlib import Path
from itertools import groupby
def by_folder(path):
return path.parent
for folder, files in groupby(Path("path/to/root/dir").rglob("*.xlsx"), key=by_folder):
print(f"Gonna merge these files from {folder}: ")
for file in files:
print(f"{file.name}")
print()
We recursively search for .xlsx files in the root directory, and group files into lists based on the immediate common parent folder. If a folder is empty, it won't be matched by the glob pattern.
You can use the os.listdir() method to list everything that's inside a folder. Only bad thing is that it'll get all files, so you may get a problem to get "inside a file" when actually you want to get inside all folder.
The following code loop through all subfolders, skip files, and print the name of all folders that are not empty.
for folder in os.listdir("."):
try:
if len( os.listdir("./"+folder) )>0:
print(
folder
)
except:
pass

Moving files to a folder in python

I have a code that locates files in a folder for me by name and moves them to another set path.
import os, shutil
files = os.listdir("D:/Python/Test")
one = "one"
two = "two"
oney = "fold1"
twoy="fold2"
def findfile(x,y):
for file in files:
if x in file.lower():
while x in file.lower():
src = ('D:/Python/Test/'+''.join(file))
dest = ('D:/Python/Test/'+y)
if not os.path.exists(dest):
os.makedirs(dest)
shutil.move(src,dest)
break
findfile(one,oney)
findfile(two,twoy)
In this case, this program moves all the files in the Test folder to another path depending on the name, let's say one as an example:
If there is a .png named one, it will move it to the fold1 folder. The problem is that my code does not distinguish between types of files and what I would like is that it excludes the folders from the search.
If there is a folder called one, don't move it to the fold1 folder, only move it if it is a folder! The other files if you have to move them.
The files in the folder contain the string one, they are not called exactly that.
I don't know if I have explained myself very well, if something is not clear leave me a comment and I will try to explain it better!
Thanks in advance for your help!
os.path.isdir(path)
os.path.isdir() method in Python is used to check whether the specified path is an existing directory or not. This method follows symbolic link, that means if the specified path is a symbolic link pointing to a directory then the method will return True.
Check with that function before.
Home this helps :)

Moving specific files (with exceptions) from a network path

I'm trying to create a script in order to move files from a list I have. I'd like to create some conditions to that but I'm afraid that's where my Python knowledge fails me. I have a list of names (AAA, BBB, CCC).
For each of those, there are six different files with six different extensions that need to be moved (AAA.1, AAA.2, AAA.3, AAA.4, AAA.5, AAA.6). Those files might be in 3 different folders. Let's suppose, either AAA/AAA or BBB/IOL or BBB/ABC. I want all of those files to be moved to REAL/AAA. The thing is, on the folder AAA/AAA there are some AAAXXX.1 files that I do not want to be moved.
I'm completely lost and new to Python (basically, it's my first week :p).
import os
import shutil
import fnmatch
source = os.listdir(r"\\enterprise\AAA\AAA")
destination = os.listdir(r"\\enterprise\REAL\AAA")
set = {
"AAA",
"BBB",
"CCC"
}
for file in source:
for x in set:
if file.__contains__(str(x)):
print(file)
I don't know how could I specify that AAAXXX, BBBXXX and etc shall not be moved.
I don't how how to insert multiple folders for searching files with conditions (If not in folder AAA/AAA, try BBB/IOL and if not BBB/ABC)
I don't know how could I specify that AAAXXX, BBBXXX and etc shall not
be moved.
The simplest way is something like this:
if 'XXX' in file:
continue # this means skip the rest of the cycle and move to next file
I don't how how to insert multiple folders for searching files with
conditions (If not in folder AAA/AAA, try BBB/IOL and if not BBB/ABC)
The most blunt approach would be
if file_name in os.listdir('first_folder'):
# move from first
elif file_name in os.listdir('second_folder'):
# move from second
# continue adding elif for every folder
else:
print(f'file {file_name) is not found')
But I'd probably just scanned every folder and then moved everything matching given name to the destination. Although this might not be what you want if you've got duplicate names and different file contents, and you have some folder particular folder precedence.

Copy file, rename it, iterate and repeat

I use os.renmae to rename files and move them about, but i am failing at doing the following task.
I have a main folder containing sub-folders with the structure below.
Main folder "Back", containing sub-folders named with letters and numbers e.g. A01, A02, B01, B02, etc.. inside each of those folders is a set of files, amongst them is a file called "rad" so a file path example looks something like this:
Back/A01/rad
/A02/rad
/B01/rad
.../rad
I have another sub-folder called "rads" inside the main "Back"
Back/rads
What i want to do is copy all the rad files only, from each of the folders in back and move them to the folder "rads" and name each rad file based on the folder it came from.
e.g. rad_A01, rad_A02, rad_B01, etc...
I couldnt really figure out how to increase the folder number when i move the files.
os.rename ("Back//rad, Back/rads/rad_")
I thought of making a list of all the names of the files and then do something like from x in list, do os.rename (), but i didnt know how to tell python to name the file according to the subfolder it came from as they are not a continuous series..
Any help is appreciated.
Thanks in advance
import os
for subdir, dirs, files in os.walk('./Back/'):
for file in files:
filepath = subdir+os.sep+file
if filepath.endswith("rad.txt"):
par_dir = os.path.split(os.path.dirname(filepath))[1]
os.system('cp '+filepath+' ./Back/rads/rad_'+par_dir)
save this python file beside Back directory and it should work fine!
This code iterates over each files in each subdirectory of Back, checks all files with name rad.txt, appends name of parent directory and copy to the rads folder.
Note: I saved rad files with .txt extension, change it accordingly.

Categories