DEV Community

Discussion on: Python for Bash

Collapse
 
kojiromike profile image
Michael A. Smith • Edited

Thanks for the nice introduction to the subprocess module. Have you looked at the pathlib module? It provides an object-oriented way to interact with the filesystem. For example, instead of parsing the stdout from ls, you can get the directory contents using pathlib:

from pathlib import Path
...
fileList = list(Path(./_drafts).iterdir())

You can also use it to move the draft to the destination.

if len(fileList) == 1:
    ...
    src = fileList[0] # already a Path object
    dst = Path(./_posts, getTodaysDate() + - + src.name)
    src.rename(dst)

So pathlib can replace all the file system subprocess calls here! (But you’d still need subprocess for the git calls, of course. There are libraries for that, but nothing in the standard library.)

It’s part of Python 3 and available on pypi for Python 2.

Collapse
 
_andy_lu_ profile image
Andy Lu

I should be the one thanking you, this is so cool!