0

I have a list of files to be used as input that is very long. I was looking at the file input module, which seems to have some attractive features, [https://docs.python.org/3/library/fileinput.html][1]

But I could not figure out how to use it without entering each file name by hand, which would be prohibitively long.

What I would love to do is submit something like

python myscript.py (ls -d -1 dir/* | grep summary.txt)

Or I could push this same bash command to filelist.txt and submit that way; either way the trick would be to submit without having to type each file.

5
  • Maybe this will work: ls -d -1 dir/* | xargs grep summary.txt | xargs python myscript.py Commented Jun 12, 2014 at 4:46
  • You should probably not be calling on an external utility to generate the file names to begin with. Python is perfectly capable of all of that. Never use ls output in a program either. Commented Jun 12, 2014 at 7:07
  • What are the contents of summary.txt? Under no circumstances should you be processing the output of ls. (Also, grep reads from standard input or a file argument, not both.) Commented Jun 12, 2014 at 12:30
  • @chepner - why "under no circumstances" could you help me understand why this is bad practice? Commented Jun 12, 2014 at 17:41
  • See mywiki.wooledge.org/ParsingLs Commented Jun 12, 2014 at 18:00

2 Answers 2

1

You could use the os package or subprocess. You would just have to figure out the exact command you would need to run in the terminal then read it and split it.

import os
files = os.popen('ls').read()
# "file1\nfile2\nfile3\nfile4"
files.rsplit()
# ['file1', 'file2', 'file3', 'file4']

Now you have a list of inputs that you can pass to the function

Sign up to request clarification or add additional context in comments.

1 Comment

Use os.listdir instead of a pipe to ls.
0

I'm pretty sure you cannot use sys.argv for this. Try using subprocess.Popen():

import subprocess
args = subprocess.Popen(['ls', '-d', '-1', 'dir/*', '|', 'grep', 'summary.txt'], stdout=subprocess.PIPE)
out, err = args.communicate()
#Do something with out

1 Comment

This won't work. ls doesn't understand globs and definitely can't create a pipe or execute the grep utility. You should be executing a shell (or better, not relying on external programs for this simple task at all).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.