This code processes directories. It first tries to rename the directory to check if it is in use.
for d in reldir:
os.rename(d, os.path.join(d + '_'))
# if original directory name exists, skip to next d
os.rename(os.path.join(d + '_'), d)
However, the behavior is the problem. I need it to stop processing the directory if the rename fails (not stop the whole script as it currently does). Directories that are not in use are processed correctly. How do I make it skip a directory in use? On batch I could just do exit /b
in the for loop. I prefer no output for these skipped directories.
This is the error I get when directory is in use:
Traceback (most recent call last):
File "D:/SYSTEM/CODING/PYTHON/import.py", line 49, in <module>
os.rename(d, os.path.join(d + '_'))
PermissionError: [WinError 5] Access is denied: 'Dir1' -> 'Dir1_'
Process finished with exit code 1