0

I'd like to create a Python script that I can pipe file names into, so it can be used like this:

ls *.csv | python .\myscript.py

I know I can read the standard input using fileinput or sys.stdin, but since the output is in a formatted table:

    Directory: C:\path\to\csv


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       15.02.2016     10:18           4755 data_1.csv
-a----       15.02.2016     10:18            522 data_2.csv

I'd have to parse it.

Since this seems to be a common pattern, I expect there's a standard solution. I'd like to avoid writing my own if I can, but I haven't been able to find any.

(In case it's important, I'm running it in PowerShell on Windows 10.)

Edit: I'm asking whether there's some library or pattern I'm missing, for this specific (and seemingly quite common) scenario of piping file paths to a script. I'd like to avoid writing parsing code if it isn't necessary.

jfs
  • 374,366
  • 172
  • 933
  • 1,594
ver
  • 858
  • 1
  • 8
  • 16
  • Possible duplicate of [How do you read from stdin in Python?](http://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python) – timgeb Feb 18 '16 at 23:07
  • I'd argue against this being a duplicate of that question. I'm asking whether there's some library or pattern I'm missing, for this specific (and seemingly quite common) scenario. – ver Feb 19 '16 at 11:32
  • 1
    You probably want to find a way to get only the file names: solve this from PowerShell, and not from Python where you would get a file per line. – Quentin Pradet Feb 19 '16 at 11:38
  • Does "ls *.csv | fl" displays only each filename without the metadata? I don't have a Windows system nearby to try this. – Quentin Pradet Feb 19 '16 at 11:43
  • is there any issue with using `myscript *.csv` instead? (`filenames = sys.argv[1:]`) – jfs Feb 19 '16 at 12:06
  • 1
    What do you want to pass to Python? Just file name: `ls *.csv | % FullName | python .\myscript.py`? – user4003407 Feb 19 '16 at 12:17

1 Answers1

0

Based on googling and your kind comments, it seems it would be best to just do the filtering before passing it to the actual Python script. I'll stick with that, then.

ver
  • 858
  • 1
  • 8
  • 16