简体   繁体   中英

How to pass arguments from file?

I have small python script like this:

import sys
import subprocess

resource_names = sys.argv[1:]
for resource in resource_names:
    command = "terraform taint " + resource
    p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    stdout, stderr = p.communicate()
print(sys.argv)

I need able to pass arguments from file:

argument1
argument2
argument3

In expectation script should be work like this:

python script.py file-with-arguments

What you can do is make a txt file with arguments in each line. Pass that filename as an argument and readlines.

import sys
import subprocess

fileName = sys.argv[1]

with open(fileName,'r') as f:
    resource_names = f.readlines()
    f.close()

for resource in resource_names:
    command = "terraform taint " + resource
    p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    stdout, stderr = p.communicate()
print(sys.argv)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM