Python Deleting All Folders but not files - python

I am looking to write a piece of python code that deletes all folders and their contents, but does not delete individual files.
For example here are some files and folders contained in a directory (Folder B) along with the script file that does the deleting. How do I delete folderA, folderB,folderC,etc, but leave the files? Thanks
/Folder B
file.docx
fileB.docx
fileC.docx
pythonDeleteScript.py
folderA/
folderB/
folderC/
folderD/

Use os.listdir() to get the contents of the directory, os.path.isdir(path) to see if it is a folder, and if it is, shutil.rmtree(path) to delete the folder and all its content.

Related

Confusing about path format if I am using os.walk

I have a folder contains both zipped and non zipped files, I want to migrate data in a given format from one place to another in the local mode.
I am new in Python so I tried os module for the first time and for accessing all the files in the folder, I am using os.walk for root path, directory list and file lists. It is showing that zipped files are not directories and normal files are directories.
When I am printing root path then it is showing in this way
Desktop/shin/archieve
Desktop/shin/archieve\New folder
My code is
for path,dir_list,file_list in os.walk('Desktop/shin/archieve'):
print(path)
I am confused about "Desktop/shin/archieve\New folder" and "Desktop/shin/archieve/New folder" and also why zipped files are considering as files only.

Deleting folder only if it does not contain any files

Is there a way to delete a folder in python if it does not contain any files? I can do the foll:
os.rmdir() will remove an empty directory.
shutil.rmtree() will delete a directory and all its contents.
If the folder has empty sub-folders, it should be deleted too
os.removedirs(path)
Remove directories recursively. Works like rmdir() except that, if the
leaf directory is successfully removed, removedirs() tries to
successively remove every parent directory mentioned in path until an
error is raised (which is ignored, because it generally means that a
parent directory is not empty).
e.g.
import os
if not os.listdir(dir):
os.removedirs(dir)
See more details from os.removedirs.
Hope this helps.

Copy and move files in Python?

What's the best way to copy and move files in python to a different directory, and overwrite those files in the destination if they already exist?
Take a look at shutil.copyfile
file1=open('insert directory here').readlines()
file2=open('insert directory here', 'w')
file2.write(file1)
If either of the directories have spaces in them do this:
directory='insert directory here'
directory=directory.rstrip()
Then use the variable directory as you would use the 'insert directory here'.

How to create a folder for each item in a directory?

I'm having trouble making folders that I create go where I want them to go. For each file in a given folder, I want to create a new folder, then put that file in the new folder. My problem is that the new folders I create are being put in the parent directory, not the one I want. My example:
def createFolder():
dir_name = 'C:\\Users\\Adrian\\Entertainment\\Coding\\Test Folder'
files = os.listdir(dir_name)
for i in files:
os.mkdir(i)
Let's say that my files in that directory are Hello.txt and Goodbye.txt. When I run the script, it makes new folders for these files, but puts them one level above, in 'C:\Users\Adrian\Entertainment\Coding.
How do I make it so they are created in the same place as the files, AKA 'C:\Users\Adrian\Entertainment\Coding\Test Folder'?
import os, shutil
for i in files:
os.mkdir(os.path.join(dir_name , i.split(".")[0]))
shutil.copy(os.path.join(dir_name , i), os.path.join(dir_name , i.split(".")[0]))
os.listdir(dir_name) lists only the names of the files, not full paths to the files. To get a path to the file, join it with dir_name:
os.mkdir(os.path.join(dir_name, i))

Python - Writing a folder and its contents to a ZipFile

Is it possible to write a folder and its contents to an existing ZipFile?
I've been messing around with this for a while and can only manage to write the folder structure to the archive, anything inside the folder isn't copied.
I don't want to point to a specific file, because the idea is that the folder contents can change and the program will just copy the whole folder into the archive no matter what is inside.
Currently I have,
myzipfile.write('A Folder\\Another Folder\\')
but I want the contents of 'Another Folder' to be copied as well not just the empty folder
Hopefully you understand what I mean.
Use os.walk:
import os
for dirpath,dirs,files in os.walk('A Folder/Another folder'):
for f in files:
fn = os.path.join(dirpath, f)
myzipfile.write(fn)

Categories

Resources