How do I do atomic updates to a directory? - python

I have a directory with some files in it (they're mostly images with a JSON file). I want to process those files, possibly overwrite some of them and possibly create some new files from some other source. I can have an error happen at any point in this process.
How do I ensure that if an error occurs as I'm processing one of the files that I won't end up with the directory in a weird state? I want to only change the contents of the directory if everything went well.
Should I create a temporary directory with tempfile.mkdtemp, put my code in a try, do my update in the "temporary" directory, swap the existing directory with the temporary directory, and delete the temporary directory if it still exists in the finally?
I'm using Python (Django).

Related

transferring files from a subdirectory

I have a subdirectory "update" and I need to move all files and folders from it to the current directory, removing all files and folders from the current directory. The problem is that there are a lot of subfolders in the subdirectory and I can't figure out how to move them all.
shutil.move recursively moves files and folders.
If you wish to copy the internal directory and extract everything, use the following line:
shutil.copytree("update", ".", dirs_exist_ok=True)
You may then safely remove the "update" directory:
shutil.rmtree("update")
If you wish to clear the current directory, I would first move the update directory outside the current one, delete the current, and then rename.
Keep in mind, that if it's your current directory, I'm guessing the directory would be in use as Python is from it, so you can't really delete that.

Removing directory in python almost always causes the folder to 'lock' and be un-removeable due to a permission error

I'm using some simple code to remove a folder structure: One folder that has multiple subfolders that are populated each time the script is run, each named 1, 2, 3, etc. Inside each subfolder is a bunch of .png files. I'm running on Win10 Pro.
When using any method to remove the files and folders, windows "locks" the subfolder "1" but successfully deletes everything else. The folder becomes impossible to remove, asking permission from my own account or the Administrators group to delete it. The script cannot delete it and throws a PermissionError when it tries.
The folder disappears after a restart. Oddly, it also disappears after about 10 minutes of waiting and not doing anything.
I've used the following methods to remove the folders without success:
shutil.rmdir() normally
shutil.rmdir(onerror=fixpermission) with a function to clear read-only errors
os.chmod(file_path, 0o777) every file in the folder, os.remove() every file in the folder, then os.rmdir folders
literally just os.rmdir-ing every subfolder

How can I delete folder which contains the script?

I need to delete folder where I have the script stored and executed from.
Ex.:
Folder:
- script.py
- some_other_content
And I need to delete the Folder. I tried using combination of shutil.rmtree() and os.rmdir() like this:
import shutil, os
path = os.path.abspath(__file__ + "/../") #points to Folder
shutil.rmtree(path)
os.rmdir(path)
But the script got deleted after shutil.rmtree(path) executes and gets removed and thus got terminated before actually removing the Folder (but still removes content of it). And if I swap the order of lines I get OSError.
Any idea?
Edit: What happens to script when i delete it in middle of execution?
System info: Windows 10, Python 3.7
There's no way to delete a whole folder that the script is residing in without deleting the script or moving it.(Don't do that) You should instead leave the script in /bin and then provide command line parameters for the folder you want deleted.
Why do you NEED to have the script reside there? The only other option I can think of is delete all the contents. Answer that and I will ammend by answer to further help.

Get working directory the process was started with

Windows and Python.
Is it possible to get the working directory that a process (not under my control) was started with, after the current working directory is changed?
I suspect Windows will lose this info irremediably, but looking for a confirmation.
As specified by eryksun:
Python adds the script directory to sys.path, not the working
directory. The Windows ProcessParameters store the DosPath string and
Handle for the working directory. All traces of the initial working
directory are removed when a new working directory is set, i.e. the
DosPath string is updated and the old directory Handle is closed and
replaced by the new one. I checked whether auditing process creation
might help, but the audit event doesn't store the initial working
directory.

Windows: Killing every process that stops a folder from being deleted

I need to delete a folder under Windows. Sometimes there are processes who prevents me from deleting said folders (their current directory is the folder, they are using one the files in the folder etc)
I'm looking for a way to loop over all processes that are using any files in my directory - then I will kill them and delete the folder.
I'm coding in Python but I guess I should be looking for some Windows Internals to do the job...

Categories

Resources