Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use xonsh to loop over files with ls

Tags:

xonsh

I want to use xonsh to bzip several files in a directory. I first attempt this with the following:

$ ls
table_aa.csv    table_amgn.csv  table_csco.csv  table_esrx.csv  table_hal.csv  table_jbl.csv  table_pcg.csv   table_zmh.csv
table_aapl.csv  table_amzn.csv  table_d.csv     table_gas.csv   table_hp.csv   table_jpm.csv  table_usb.csv
$ for fn in ls:
..    bzip2 fn
..
NameError: name 'ls' is not defined

OK, so I use $() explicitly

$ for fn in $(ls).split():
.     bzip2 fn
bzip2: Can't open input file fn: No such file or directory.
bzip2: Can't open input file fn: No such file or directory.

Is there a better way to do this?

$ xonsh --version
('xonsh/0.3.4',)
like image 481
MRocklin Avatar asked Jan 05 '23 20:01

MRocklin


1 Answers

You are very close with the second example. The only thing to note is that fn is a Python variable name, so you have to use @() to pass it to a subprocess:

$ for fn in $(ls).split(): . bzip2 @(fn)

Also, on v0.3.4, you could use regex globbing instead of ls,

$ for fn in `.*`: . bzip2 @(fn)

And at least on master, you can now iterate through !() line-by-line, which means that the following will also work if you are wedded to ls:

$ for fn in !(ls): . bzip2 @(fn)

like image 134
Anthony Scopatz Avatar answered Feb 11 '23 06:02

Anthony Scopatz