On Mon, Feb 1, 2021 at 1:16 PM James H. H. Lampert
<jamesl@xxxxxxxxxxxxxxxxx> wrote:
Just out of morbid curiosity, are there other solutions (that wouldn't
involve spending all day researching API calls and writing a program to
use them)?
For the tasks you asked for, Python is not as concise as the
Unix-style commands, but probably much more readable (and to my eye,
more readable than the SQL as well).
If you don't need to inspect subdirectories, it's supremely readable:
# Find anything (files or directories) in MyDir that don't end in .zip
import os
for name in os.listdir(MyDir):
if not name.endswith('.zip'):
print(name)
# Find all directories in MyDir
import os
for name in os.listdir(MyDir):
if os.path.isdir(name):
print(name)
If you do need to recursively inspect subdirectories, the usual way is
to use os.walk, whose interface is (understandably) not quite as
simple as os.listdir, but ultimately still roughly as concise:
# Find files (but not directories) in MyDir that don't end in .zip
import os
for root, dirs, files in os.walk(MyDir):
for name in files:
if not name.endswith('.zip'):
print(os.path.join(root, name))
# Find all directories in MyDir
import os
for root, dirs, files in os.walk(MyDir):
print(root)
In the last two snippets above, dirs is a list of subdirectories,
which you can actually alter on the fly if you don't want to traverse
every single subdirectory.
As Kevin alluded to for the Unix-style commands, you can easily write
to source members if you really want to. But most likely, if you're
going to use Python at all, you might as well just do whatever
processing you're going to do (delete things, rename things, whatever)
within Python. (In the above snippets, replace print with whatever
processing you want.)
A nice thing about Python scripts is that they tend to be easier to
expand upon as your needs evolve (hey, that one-time task worked out
so well for Department A that we're going to flesh it out, make it
work for all departments, and run it every month), compared to the
Unix one-liners, and arguably compared to the SQL as well. And if you
really are more comfortable doing things with SQL (which really is the
best for some things), you can certainly do that within Python. In
many ways, it's actually even easier to use SQL within Python than it
is to use SQL within RPG.
John Y.
As an Amazon Associate we earn from qualifying purchases.