Command-line handling
Fatal errors should be reported on sys.stderr
and cause a non-zero exit status.
Don't check for os.path.exists(dest)
, because:
- Existence doesn't actually guarantee that it is a directory rather than a regular file.
os.makedirs(dest)
will perform that check anyway.
- Even if you
os.path.exists()
finds a directory, there is a slim chance that it might get deleted in a race condition.
If you want to catch IndexError
instead of checking for the length of sys.argv
, then I suggest that you do so consistently for both arguments:
from __future__ import print_function
try:
m3ufile = sys.argv.pop(1)
except IndexError:
print("No m3u file given", file=sys.stderr)
sys.exit(1)
try:
dest = sys.argv.pop(1)
except IndexError:
dest = '.'
else:
os.makedirs(dest)
File input
t
is a funny variable name. How about line
instead?
t not in ['\n','\r\n']
is superfluous, since if t
performs the same check.
files = []
with open(m3ufile) as f:
for line in f:
line = line.strip()
if line and line[0] != '#':
files.append(line)
(This isn't exactly equivalent to your original code, in that it also treats #
as a comment even if it is preceded by whitespace. I consider that to be an improvement.)
Copying
The Esc[2J
VT100 escape code deserves a comment. Also consider using curses
for clarity and generality.
You shouldn't declare that something is done until it's done, especially if there's a chance that you might not do it at all. In fact, I would expect that a missing file would be considered an error.
I suggest using itertools.count()
to track the progress.
from itertools import count
progress, goal = count(1), len(files)
skipped = []
for path in files:
# Esc [2J is the VT100 sequence to erase the screen and move the cursor to
# the beginning of the line
if os.path.exists(path):
shutil.copy(path, dest)
print("\x1b[2J{0} of {1} collected!!".format(next(progress), goal))
else:
skipped.append(path)
if skipped:
print("Missing files:", file=sys.stderr)
for path in skipped:
print(path, file=sys.stderr)
sys.exit(2)
else:
print("All files collected in {0} directory. Enjoy!".format(dest))