street_jesus
street_jesus

Reputation: 383

Passing Command Line Arguments From a File in Python

I wrote a python script to run my vb.net project executable. The problem I ran into is when I read the lines and then use a for loop to use the individual line (IP) as an argument to my application, this does not work. However when I use any other String it does work. So it does not read the IP from ipaddress.txt but it does take an IP if entered manual as the argument instead of 'line'.

import subprocess
f = open('ipaddress.txt')
lines = f.readlines()
f.close()
for line in lines:
   subprocess.Popen(['C:\\Users\\YYYYY\\OneDrive - XXXX
   \\Visual-Studio-Projects\\Map_Tester\\bin\\Map200_Tester.exe', 'line'])

Any details why this is happening are appreciated. Thanks.

Upvotes: 0

Views: 104

Answers (1)

d_kennetz
d_kennetz

Reputation: 5359

In your code you have 'line' enclosed in quotes signifying it as a string. But you want line to be read as a variable, so simply remove the quotes in line in your for loop, which will do what you want: iterate over each for line in lines:

import subprocess
f = open('ipaddress.txt')
lines = f.readlines()
f.close()
for line in lines:
    subprocess.Popen(['C:\\Users\\YYYYY\\OneDrive - XXXX
    \\Visual-Studio-Projects\\Map_Tester\\bin\\Map200_Tester.exe'], line[:-1])

This edit would remove any newline character from your line variable.

Upvotes: 1

Related Questions