I'll have to move a large network directory with many sub-directories and thousands of files. A number of users work on these files on a daily basis. OS is windows.
The move itself (copy and delete) I'll do using
Before starting the batch process of moving, I would like to check for locked files. In case some files or folders are locked, I won't bother starting the process.
robocopy has a dry run switch, but that doesn't check for locked files which would lead to an unsuccessful run.
I quickly came up with something like the below, in Python:
import os is_locked=list() for root, dirnames, filenames in os.walk('path/to/directory'): for filename in filenames: full_filename = os.path.join(root, filename) try: pf = open(full_filename, 'a') pf.close() except PermissionError: is_locked.append(full_filename)
Your opinion whether this is a good or bad way of doing it? Any general comment? Performance, etc?
Is it harmless to be opening these files?
--- edit ---
Summary of the most relevant comments:
- Break the loop when first locked file is found.