Making Python scripts work with xargs

2024/9/8 10:47:02

What would be the process of making my Python scripts work well with 'xargs'? For instance, I would like the following command to work through each line of text file, and execute an arbitrary command:

cat servers.txt | ./hardware.py -m 

Essentially would like each line to be passed to the hardware.py script.

Answer

To make your commands work with xargs you simply need them to accept arguments. Arguments in Python are in the sys.argv list. In this way you can execute somthing like:

find . -type f -name '*.txt' -print0 | xargs -0 ./myscript.py

which might be equivalent to

./myscript.py ./foo.txt ./biz/foobar.txt ./baz/yougettheidea.txt

To make your commands work with standard input, you can also use the sys module, this time with sys.stdin, which you can treat as a file. This is more like the example you gave:

./myscript.py < somefile.txt
https://en.xdnf.cn/q/72427.html

Related Q&A

TypeError: expected string or buffer in Google App Engines Python

I want to show the content of an object using the following code:def get(self):url="https://www.googleapis.com/language/translate/v2?key=MY-BILLING-KEY&q=hello&source=en&target=ja&quo…

Returning a row from a CSV, if specified value within the row matches condition

Ahoy, Im writing a Python script to filter some large CSV files.I only want to keep rows which meet my criteria.My input is a CSV file in the following formatLocus Total_Depth Average_Depth_sa…

Python multiprocessing pool: dynamically set number of processes during execution of tasks

We submit large CPU intensive jobs in Python 2.7 (that consist of many independent parallel processes) on our development machine which last for days at a time. The responsiveness of the machine slows …

TypeError: cant escape psycopg2.extensions.Binary to binary

I try to store binary file into postgresql through sqlalchemy and file is uploaded from client. A bit google on the error message brings me to this source file:" wrapped object is not bytes or a…

Keras: Cannot Import Name np_utils [duplicate]

This question already has answers here:ImportError: cannot import name np_utils(19 answers)Closed 6 years ago.Im using Python 2.7 and a Jupyter notebook to do some basic machine learning. Im following…

Python 3 string index lookup is O(1)?

Short story:Is Python 3 unicode string lookup O(1) or O(n)?Long story:Index lookup of a character in a C char array is constant time O(1) because we can with certainty jump to a contiguous memory loca…

Using PIL to detect a scan of a blank page

So I often run huge double-sided scan jobs on an unintelligent Canon multifunction, which leaves me with a huge folder of JPEGs. Am I insane to consider using PIL to analyze a folder of images to detec…

Pandas: Filling data for missing dates

Lets say Ive got the following table:ProdID Date Val1 Val2 Val3 Prod1 4/1/2019 1 3 4 Prod1 4/3/2019 2 3 54 Prod1 4/4/2019 3 4 54 Prod2 4/1/2019 1 3 3…

Linear Regression: How to find the distance between the points and the prediction line?

Im looking to find the distance between the points and the prediction line. Ideally I would like the results to be displayed in a new column which contains the distance, called Distance.My Imports:impo…

How to draw a Tetrahedron mesh by matplotlib?

I want to plot a tetrahedron mesh by matplotlib, and the following are a simple tetrahedron mesh: xyz = np.array([[-1,-1,-1],[ 1,-1,-1], [ 1, 1,-1],[-1, 1,-1],[-1,-1, 1],[ 1,-1, 1], [ 1, 1, 1],[-1, 1, …