This question already has answers here:
How to delete the contents of a folder?
(26 answers)
Closed 1 year ago.
I am trying to create a python script to help clean up a folder that I create with powershell. This is a directory that contains folders named after the users for them to put stuff into.
Once they leave our site, their folder remains and for all new staff who come a folder gets created. This means that we have over 250 folders but only 100 active staff. I have a test folder that I am using so that I can get the script working and then add in extra bits like moving the directories to an old location, and then deleting them based on age. But at the moment I am working on the delete portion of the script. So far I have the below script, it runs with no errors, but it doesn't actually do anything and I am failing to see why..
It should be reading a csv file that I have of all current staff members, and then comparing that to the folders located in the filepath and then removing any folders that dont match the names in the CSV file.
The CSV file is generated from powershell using the same script that I used to create them.
import os
import pandas as pd
path = "//servername/Vault/Users$"
flist = pd.read_csv('D:/Staff List.csv')
file_name = flist['fileName'].tolist()
for fileName in os.listdir(path):
#If file is not present in list
if fileName not in file_name:
#Get full path of file and remove it
full_file_path = os.path.join(path, fileName)
os.remove(full_file_path)
Use shutil and recursively remove all old user directories and their contents.
import shutil
PATH = 'D:/user/bin/old_dir_to_remove'
shutil.rmtree(PATH)
Related
This question already has answers here:
Extract files from zip file and retain mod date?
(4 answers)
Closed last year.
I'm unzipping hundreds of zipped files with python as explained here.
import os
import zipfile
base_dir = '/users/me/myFile' # absolute path to the data folder
extension = ".zip"
os.chdir(base_dir) # change directory from working dir to dir with files
def unpack_all_in_dir(_dir):
for item in os.listdir(_dir): # loop through items in dir
abs_path = os.path.join(_dir, item) # absolute path of dir or file
if item.endswith(extension): # check for ".zip" extension
file_name = os.path.abspath(abs_path) # get full path of file
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
zip_ref.extractall(_dir) # extract file to dir
zip_ref.close() # close file
elif os.path.isdir(abs_path):
unpack_all_in_dir(abs_path) # recurse this function with inner folder
unpack_all_in_dir(base_dir)
When I unzip a file manually it will get its original modification date, whereas when doing it with code I loose this - the modification date turns into now's date.
Any idea of a way the preserve the original creation date?
I don't know zipfile very well, but according to this thread about modification date, preserving metadata is a pain.
You can hack around with calling some CLI archiving program as a subprocess, but you need to make sure that it's installed on the target system. I actually had to bundle 7zip with my script once, because of some issue with Python libraries, even third-party ones
Actually opening multiple zip files manually keeps the dates, at least on macOS.
Search for the files you need to unzip, select -> open.
Every month I have to make what we call a patch build at work. All it is, is a specific file structure of folders and subfolders with the current months Microsoft updates in there, along with a script to run it all from an excel file for our servers.
I am just trying to automate the file structure part of it by making a small program that will make a new directory copied from the previous month. Then change all folders with the previous months name in it to the current month. below is what I have so far it works great. Now I just need to isolate parts of the folder names and change them throughout the entire directories structure.
My question is how do I search through a folder and all sub folders and change just one part of the folder names. In this case any folders the May in it need to be changed to June
import os, fnmatch
from pathlib import Path
import shutil
src_path = "C:\CND_Patch_Builds\May 2021 Patches" #path to copy
dst_path = "C:\CND_Patch_Builds\June 2021 Patches" #path to create and or list the directories and subdirectories of
os.chdir(src_path) #changes from root directory to working directory src_path
print(os.getcwd())
def listdirs(dst_path): #function that list a directory and all its subdirectories
for folder in Path(dst_path).iterdir():
if folder.is_dir():
print(folder)
listdirs(folder)
if not os.path.isdir(dst_path): #if statement that copies the src_path and makes a new one under the dst_path if dst_path doesn't already exist
copy_patch = shutil.copytree(src_path, dst_path)
shutil.move(copy_patch, dst_path)
print(f"{dst_path} has been created")
print("Directory and Subdirectories listed below\n")
listdirs(dst_path)
else:
print(f"{dst_path} exists\n") #uses the listdirs function if dst_path already exists
listdirs(dst_path
This question already has answers here:
Rename multiple files inside multiple folders
(3 answers)
Closed 4 years ago.
i'm trying to erase all indexes (characters) except the last 4 ones and the files' extension in python. for example:
a2b-0001.tif to 0001.tif
a3tcd-0002.tif to 0002.tif
as54d-0003.tif to 0003.tif
Lets say that folders "a", "b" and "c" which contains those tifs files are located in D:\photos
there many of those files in many folders in D:\photos
that's where i got so far:
import os
os.chdir('C:/photos')
for dirpath, dirnames, filenames in os.walk('C:/photos'):
os.rename (filenames, filenames[-8:])
why that' not working?
So long as you have Python 3.4+, pathlib makes it extremely simple to do:
import pathlib
def rename_files(path):
## Iterate through children of the given path
for child in path.iterdir():
## If the child is a file
if child.is_file():
## .stem is the filename (minus the extension)
## .suffix is the extension
name,ext = child.stem, child.suffix
## Rename file by taking only the last 4 digits of the name
child.rename(name[-4:]+ext)
directory = pathlib.Path(r"C:\photos").resolve()
## Iterate through your initial folder
for child in directory.iterdir():
## If the child is a folder
if child.is_dir():
## Rename all files within that folder
rename_files(child)
Just note that because you're truncating file names, there may be collisions which may result in files being overwritten (i.e.- files named 12345.jpg and 22345.jpg will both be renamed to 2345.jpg, with the second overwriting the first).
This question already has answers here:
How to create a zip archive of a directory?
(28 answers)
Closed 1 year ago.
I've got a folder called: 'files' which contains lots of jpg photographs. I've also got a file called 'temp.kml'. I want to create a KMZ file (basically a zip file) which contains the temp.kml file AND the files directory which has the photographs sitting inside it.
Here is my code:
zfName = 'simonsZip.kmz'
foo = zipfile.ZipFile(zfName, 'w')
foo.write("temp.kml")
foo.close()
os.remove("temp.kml")
This creates the kmz file and puts the temp.kml inside. But I also want to put the folder called 'files' in as well. How do I do this?
I read here on StackOverflow that some people have used shutil to make zip files. Can anyone offer a solution?
You can use shutil
import shutil
shutil.make_archive("simonsZip", "zip", "files")
The zipfile module in python has no support for adding a directory with file so you need to add the files one by one.
This is an (untested) example of how that can be achieved by modifying your code example:
import os
zfName = 'simonsZip.kmz'
foo = zipfile.ZipFile(zfName, 'w')
foo.write("temp.kml")
# Adding files from directory 'files'
for root, dirs, files in os.walk('files'):
for f in files:
foo.write(os.path.join(root, f))
foo.close()
os.remove("temp.kml")
This is my first time using python and I keep running into error 183. The script I created searches the network for all '.py' files and copies them to my backup drive. Please don't laugh at my script as this is my first.
Any clue to what I am doing wrong in the script?
import os
import shutil
import datetime
today = datetime.date.today()
rundate = today.strftime("%Y%m%d")
for root,dirr,filename in os.walk("p:\\"):
for files in filename:
if files.endswith(".py"):
sDir = os.path.join(root, files)
dDir = "B:\\Scripts\\20120124"
modname = rundate + '_' + files
shutil.copy(sDir, dDir)
os.rename(os.path.join(dDir, files), os.path.join(dDir, modname))
print "Renamed %s to %s in %s" % (files, modname, dDir)
I'm guessing you are running the script on windows. According to the list of windows error codes error 183 is ERROR_ALREADY_EXISTS
So I would guess the script is failing because you're attempting to rename a file over an existing file.
Perhaps you are running the script more than once per day? That would result in all the destination files already being there, so the rename is failing when the script is run additional times.
If you specifically want to overwrite the files, then you should probably delete them using os.unlink first.
Given the fact that error 183 is [Error 183] Cannot create a file when that file already exists, you're most likely finding 2 files with the same name in the os.walk() call and after the first one is renamed successfully, the second one will fail to be renamed to the same name so you'll get a file already exists error.
I suggest a try/except around the os.rename() call to treat this case (append a digit after the name or something).
[Yes, i know it's been 7 years since this question was asked but if I got here from a google search maybe others are reaching it too and this answer might help.]
I just encounter the same issue, when you trying to rename a folder with a folder that existed in the same directory has the same name, Python will raise an error.
If you trying to do that in Windows Explorer, it will ask you if you want to merge these two folders. however, Python doesn't have this feature.
Below is my codes to achieve the goal of rename a folder while a same name folder already exist, which is actually merging folders.
import os, shutil
DEST = 'D:/dest/'
SRC = 'D:/src/'
for filename in os.listdir(SRC): # move files from SRC to DEST folder.
try:
shutil.move(SRC + filename, DEST)
# In case the file you're trying to move already has a copy in DEST folder.
except shutil.Error: # shutil.Error: Destination path 'D:/DEST/xxxx.xxx' already exists
pass
# Now delete the SRC folder.
# To delete a folder, you have to empty its files first.
if os.path.exists(SRC):
for i in os.listdir(SRC):
os.remove(os.path.join(SRC, i))
# delete the empty folder
os.rmdir(SRC)