1
\$\begingroup\$

This script checks specific directories for *.pgp files. It returns a list of the filenames and modified dates. It then returns a list of the filenames and modified dates, each on a new line. I'm new to Python, like literally two weeks now and this script was written for a specific work purpose, but I'm betting it could be tweaked.

import glob
import os
from datetime import datetime


monitor_files = []

os.chdir("/home/skeer/Documents/Projects/Python_temp/home/adr01/upload/")
for file in glob.glob("*.pgp"):
    if os.path.isfile(file):
        last_modified_date = datetime.fromtimestamp(os.path.getmtime(file))
    else:
        last_modified_date = datetime.fromtimestamp(0)
    monitor_files.append("adr01 | " + file + (" | ") + str(last_modified_date) + "\n")

os.chdir("/home/skeer/Documents/Projects/Python_temp/home/chpt/upload/")
for file in glob.glob("*.pgp"):
    if os.path.isfile(file):
        last_modified_date = datetime.fromtimestamp(os.path.getmtime(file))
    else:
        last_modified_date = datetime.fromtimestamp(0)
    monitor_files.append("chpt | " + file + (" | ") + str(last_modified_date) + "\n")

os.chdir("/home/skeer/Documents/Projects/Python_temp/home/emb01/upload/")
for file in glob.glob("*.pgp"):
    if os.path.isfile(file):
        last_modified_date = datetime.fromtimestamp(os.path.getmtime(file))
    else:
        last_modified_date = datetime.fromtimestamp(0)
    monitor_files.append("emb01 | " + file + (" | ") + str(last_modified_date) + "\n")

os.chdir("/home/skeer/Documents/Projects/Python_temp/home/exp01/upload/")
for file in glob.glob("*.pgp"):
    if os.path.isfile(file):
        last_modified_date = datetime.fromtimestamp(os.path.getmtime(file))
    else:
        last_modified_date = datetime.fromtimestamp(0)
    monitor_files.append("exp01 | " + file + (" | ") + str(last_modified_date) + "\n")

os.chdir("/home/skeer/Documents/Projects/Python_temp/home/iix01/upload/")
for file in glob.glob("*.pgp"):
    if os.path.isfile(file):
        last_modified_date = datetime.fromtimestamp(os.path.getmtime(file))
    else:
        last_modified_date = datetime.fromtimestamp(0)
    monitor_files.append("iix01 | " + file + (" | ") + str(last_modified_date) + "\n")

print("\n".join(monitor_files))
\$\endgroup\$

migrated from stackoverflow.com Jul 2 '17 at 19:12

This question came from our site for professional and enthusiast programmers.

  • \$\begingroup\$ Thanks Jamal, I did not find out about codereview until after I posted this, thanks for moving. \$\endgroup\$ – Skeer Jul 3 '17 at 5:27
1
\$\begingroup\$

Some improvement (DRY - Don't Repeat Yourself):

import glob
import os
import os.path                    # another import
from datetime import datetime

monitor_files = []

# Instead of your 5-time repetition then use this part:

PREFIX = "/home/skeer/Documents/Projects/Python_temp/home"
subdirs = ["chpt", "adr01", "emb01", "exp01", "iix01"]       # We will loop over these
UP = "upload"
PATTERN = "*.pgp"

for sd in subdirs:
    full_path = os.path.join(PREFIX, sd, UP)        # Inteligent joining of path's parts
    os.chdir(full_path)
    for file in glob.glob(PATTERN):
        if os.path.isfile(file):
            last_modified_date = datetime.fromtimestamp(os.path.getmtime(file))
        else:
            last_modified_date = datetime.fromtimestamp(0)
        monitor_files.append(sd + " | " + file + (" | ") + str(last_modified_date) + "\n")

print("\n".join(monitor_files))   # Your code after repetitions is OK
\$\endgroup\$
1
\$\begingroup\$

Instead of gluing string with + from literals and variables, like this

"adr01 | " + file + (" | ") + str(last_modified_date) + "\n"

you may use

"adr01 | {} | {}\n".format(file, str(last_modified_date))

where {} are placeholders for variable parts of the string - parameters of the format() method.


Nicer use:

"adr01 | {file} | {date}\n".format(file=file, date=str(last_modified_date))

and in Python 3.6+ you may write it without the format() method:

f"adr01 | {file} | {str(last_modified_date)}\n"

(note the letter f - for "format" - just before the opening ")

\$\endgroup\$
  • \$\begingroup\$ Why isn't this advice incorporated into your other answer? \$\endgroup\$ – Mathieu Guindon Aug 29 '17 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.