'How to replicate powershell pipeline for file system objects in python
I'm making the transition to Python from PowerShell and Javscript and am wondering how I can replicate the PowerShell pipeline in Python. In powershell, you can easily write a command that accepts file system entities (files and/or diretories) and then sends that back out to the pipeline. So you can compose a pipeline with various different commands. The concept of a pipeline is nothing new to linux/python, I am just trying to figure out the best Pythonic way of doing this.
In Python, we have the standard command os.scandir, which returns an iterator of os.DirEntry objects. But how to we send these down a pipeline and also read these instances from the pipeline? Is there a standard way of doing this kind of thing, perhaps a standard command of which I am not yet aware (even after having searched for one).
What I'm ultimately looking for is a way of writing python functions that can be chained together via the pipeline that can accept a collection of files/directories process them in some way, then send them out to the pipeline, in a fashion similar to PowerShell. In PowerShell, files/directories are represented by objects (System.IO.FileSystemInfo), so this interface is clear, in Python this is a mystery to me. Perhaps between different commands, the interface would simply be files/directories represented by text, that would have to processed by regular expressions or some other functionality.
Actually, to be clear, I'm not necessarily interested in piping the contents of a file, more just the meta data that represents the file, ie the os.DirEntry instance.
Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
