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.
python python-3.x
add a comment |
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.
python python-3.x
add a comment |
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.
python python-3.x
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.
python python-3.x
python python-3.x
asked Nov 19 '18 at 21:21
Siraj AhmadzaiSiraj Ahmadzai
94
94
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
this still didn't work :(
– Siraj Ahmadzai
Nov 19 '18 at 21:48
see my edit, your lines could contain newlinen
characters, this would strip those without messing up the format of your code. If this doesn't work, trystr(line)
orstr(line[:-1])
– d_kennetz
Nov 19 '18 at 21:49
I think it has something to do with the fact that I read lines from the text and then use a single line as my argument whereas I should use a single word.
– Siraj Ahmadzai
Nov 19 '18 at 21:50
do the lines of your file contain more than the ipaddress string?
– d_kennetz
Nov 19 '18 at 21:52
no, the file contains ipaddresses line by line
– Siraj Ahmadzai
Nov 19 '18 at 21:52
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382808%2fpassing-command-line-arguments-from-a-file-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
this still didn't work :(
– Siraj Ahmadzai
Nov 19 '18 at 21:48
see my edit, your lines could contain newlinen
characters, this would strip those without messing up the format of your code. If this doesn't work, trystr(line)
orstr(line[:-1])
– d_kennetz
Nov 19 '18 at 21:49
I think it has something to do with the fact that I read lines from the text and then use a single line as my argument whereas I should use a single word.
– Siraj Ahmadzai
Nov 19 '18 at 21:50
do the lines of your file contain more than the ipaddress string?
– d_kennetz
Nov 19 '18 at 21:52
no, the file contains ipaddresses line by line
– Siraj Ahmadzai
Nov 19 '18 at 21:52
|
show 2 more comments
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.
this still didn't work :(
– Siraj Ahmadzai
Nov 19 '18 at 21:48
see my edit, your lines could contain newlinen
characters, this would strip those without messing up the format of your code. If this doesn't work, trystr(line)
orstr(line[:-1])
– d_kennetz
Nov 19 '18 at 21:49
I think it has something to do with the fact that I read lines from the text and then use a single line as my argument whereas I should use a single word.
– Siraj Ahmadzai
Nov 19 '18 at 21:50
do the lines of your file contain more than the ipaddress string?
– d_kennetz
Nov 19 '18 at 21:52
no, the file contains ipaddresses line by line
– Siraj Ahmadzai
Nov 19 '18 at 21:52
|
show 2 more comments
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.
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.
edited Nov 19 '18 at 21:57
answered Nov 19 '18 at 21:30
d_kennetzd_kennetz
2,0562719
2,0562719
this still didn't work :(
– Siraj Ahmadzai
Nov 19 '18 at 21:48
see my edit, your lines could contain newlinen
characters, this would strip those without messing up the format of your code. If this doesn't work, trystr(line)
orstr(line[:-1])
– d_kennetz
Nov 19 '18 at 21:49
I think it has something to do with the fact that I read lines from the text and then use a single line as my argument whereas I should use a single word.
– Siraj Ahmadzai
Nov 19 '18 at 21:50
do the lines of your file contain more than the ipaddress string?
– d_kennetz
Nov 19 '18 at 21:52
no, the file contains ipaddresses line by line
– Siraj Ahmadzai
Nov 19 '18 at 21:52
|
show 2 more comments
this still didn't work :(
– Siraj Ahmadzai
Nov 19 '18 at 21:48
see my edit, your lines could contain newlinen
characters, this would strip those without messing up the format of your code. If this doesn't work, trystr(line)
orstr(line[:-1])
– d_kennetz
Nov 19 '18 at 21:49
I think it has something to do with the fact that I read lines from the text and then use a single line as my argument whereas I should use a single word.
– Siraj Ahmadzai
Nov 19 '18 at 21:50
do the lines of your file contain more than the ipaddress string?
– d_kennetz
Nov 19 '18 at 21:52
no, the file contains ipaddresses line by line
– Siraj Ahmadzai
Nov 19 '18 at 21:52
this still didn't work :(
– Siraj Ahmadzai
Nov 19 '18 at 21:48
this still didn't work :(
– Siraj Ahmadzai
Nov 19 '18 at 21:48
see my edit, your lines could contain newline
n
characters, this would strip those without messing up the format of your code. If this doesn't work, try str(line)
or str(line[:-1])
– d_kennetz
Nov 19 '18 at 21:49
see my edit, your lines could contain newline
n
characters, this would strip those without messing up the format of your code. If this doesn't work, try str(line)
or str(line[:-1])
– d_kennetz
Nov 19 '18 at 21:49
I think it has something to do with the fact that I read lines from the text and then use a single line as my argument whereas I should use a single word.
– Siraj Ahmadzai
Nov 19 '18 at 21:50
I think it has something to do with the fact that I read lines from the text and then use a single line as my argument whereas I should use a single word.
– Siraj Ahmadzai
Nov 19 '18 at 21:50
do the lines of your file contain more than the ipaddress string?
– d_kennetz
Nov 19 '18 at 21:52
do the lines of your file contain more than the ipaddress string?
– d_kennetz
Nov 19 '18 at 21:52
no, the file contains ipaddresses line by line
– Siraj Ahmadzai
Nov 19 '18 at 21:52
no, the file contains ipaddresses line by line
– Siraj Ahmadzai
Nov 19 '18 at 21:52
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382808%2fpassing-command-line-arguments-from-a-file-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown