Read a text file contents from second line in python












-1















I wrote a python code which executes the bash command and save the output in txt file, now i want to read the txt file which actually contains details from line 2 always, and print true if output exists and send a mail to the user.



Looking for a further solution how I can read specific second line output of file output.txt and print true if the line exists to send a mail to the user otherwise goodbye.



Am very new to this approach, please help with your guidance.
TIA.



import subprocess
p = subprocess.Popen("bash command > output.txt", stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
p_status = p.wait()
print "command exit status/return code: ", p_status


Above code prints the output in the txt file in the below format



PID  PPID %CPU     ELAPSED COMMAND
3435 1 0 4-05:20:09


Requirement: Read from "3435 1 0 4-05:20:09 " this line and print true as line exist and send a mail to user. If not output after "PID PPID %CPU ELAPSED COMMAND" it should send no mail










share|improve this question

























  • I'm sorry, I think this question is too broad: Do you want help with saving the output? Reading from your saved output? Sending the mail? Those are all separate problems I'm afraid.

    – finefoot
    Dec 31 '18 at 12:46











  • Hi, i want read from saved output in this case, it is output.txt file which has above output starts from 3435 from the second line, i want to read that and send mail to user if that output exists, if no output in the second line do nothing

    – Python
    Dec 31 '18 at 12:48








  • 1





    This is just my personal feedback to your question: You should try to divide your problem in smaller parts. And then solve those small parts. Sending the mail has nothing to do with reading from a text file. It's irrelevant and distracts from the solution. You'll get much clearer questions and better answers here on StackOverflow. :-)

    – finefoot
    Dec 31 '18 at 12:51











  • ohh okie, i got your feedback now, sure thanks for the comments, sure i will do it. Actually i thought it will be included in the same code so added that part here as well, but thanks for reviewing.

    – Python
    Dec 31 '18 at 12:56











  • Always include as little code as possible, yet as much as necessary to understand the problem. Have a look at How to create a Minimal, Complete, and Verifiable example :-) Just don't give up and keep improving step by step. Good luck!

    – finefoot
    Dec 31 '18 at 13:03
















-1















I wrote a python code which executes the bash command and save the output in txt file, now i want to read the txt file which actually contains details from line 2 always, and print true if output exists and send a mail to the user.



Looking for a further solution how I can read specific second line output of file output.txt and print true if the line exists to send a mail to the user otherwise goodbye.



Am very new to this approach, please help with your guidance.
TIA.



import subprocess
p = subprocess.Popen("bash command > output.txt", stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
p_status = p.wait()
print "command exit status/return code: ", p_status


Above code prints the output in the txt file in the below format



PID  PPID %CPU     ELAPSED COMMAND
3435 1 0 4-05:20:09


Requirement: Read from "3435 1 0 4-05:20:09 " this line and print true as line exist and send a mail to user. If not output after "PID PPID %CPU ELAPSED COMMAND" it should send no mail










share|improve this question

























  • I'm sorry, I think this question is too broad: Do you want help with saving the output? Reading from your saved output? Sending the mail? Those are all separate problems I'm afraid.

    – finefoot
    Dec 31 '18 at 12:46











  • Hi, i want read from saved output in this case, it is output.txt file which has above output starts from 3435 from the second line, i want to read that and send mail to user if that output exists, if no output in the second line do nothing

    – Python
    Dec 31 '18 at 12:48








  • 1





    This is just my personal feedback to your question: You should try to divide your problem in smaller parts. And then solve those small parts. Sending the mail has nothing to do with reading from a text file. It's irrelevant and distracts from the solution. You'll get much clearer questions and better answers here on StackOverflow. :-)

    – finefoot
    Dec 31 '18 at 12:51











  • ohh okie, i got your feedback now, sure thanks for the comments, sure i will do it. Actually i thought it will be included in the same code so added that part here as well, but thanks for reviewing.

    – Python
    Dec 31 '18 at 12:56











  • Always include as little code as possible, yet as much as necessary to understand the problem. Have a look at How to create a Minimal, Complete, and Verifiable example :-) Just don't give up and keep improving step by step. Good luck!

    – finefoot
    Dec 31 '18 at 13:03














-1












-1








-1








I wrote a python code which executes the bash command and save the output in txt file, now i want to read the txt file which actually contains details from line 2 always, and print true if output exists and send a mail to the user.



Looking for a further solution how I can read specific second line output of file output.txt and print true if the line exists to send a mail to the user otherwise goodbye.



Am very new to this approach, please help with your guidance.
TIA.



import subprocess
p = subprocess.Popen("bash command > output.txt", stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
p_status = p.wait()
print "command exit status/return code: ", p_status


Above code prints the output in the txt file in the below format



PID  PPID %CPU     ELAPSED COMMAND
3435 1 0 4-05:20:09


Requirement: Read from "3435 1 0 4-05:20:09 " this line and print true as line exist and send a mail to user. If not output after "PID PPID %CPU ELAPSED COMMAND" it should send no mail










share|improve this question
















I wrote a python code which executes the bash command and save the output in txt file, now i want to read the txt file which actually contains details from line 2 always, and print true if output exists and send a mail to the user.



Looking for a further solution how I can read specific second line output of file output.txt and print true if the line exists to send a mail to the user otherwise goodbye.



Am very new to this approach, please help with your guidance.
TIA.



import subprocess
p = subprocess.Popen("bash command > output.txt", stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
p_status = p.wait()
print "command exit status/return code: ", p_status


Above code prints the output in the txt file in the below format



PID  PPID %CPU     ELAPSED COMMAND
3435 1 0 4-05:20:09


Requirement: Read from "3435 1 0 4-05:20:09 " this line and print true as line exist and send a mail to user. If not output after "PID PPID %CPU ELAPSED COMMAND" it should send no mail







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 13:28







Python

















asked Dec 31 '18 at 12:41









PythonPython

23




23













  • I'm sorry, I think this question is too broad: Do you want help with saving the output? Reading from your saved output? Sending the mail? Those are all separate problems I'm afraid.

    – finefoot
    Dec 31 '18 at 12:46











  • Hi, i want read from saved output in this case, it is output.txt file which has above output starts from 3435 from the second line, i want to read that and send mail to user if that output exists, if no output in the second line do nothing

    – Python
    Dec 31 '18 at 12:48








  • 1





    This is just my personal feedback to your question: You should try to divide your problem in smaller parts. And then solve those small parts. Sending the mail has nothing to do with reading from a text file. It's irrelevant and distracts from the solution. You'll get much clearer questions and better answers here on StackOverflow. :-)

    – finefoot
    Dec 31 '18 at 12:51











  • ohh okie, i got your feedback now, sure thanks for the comments, sure i will do it. Actually i thought it will be included in the same code so added that part here as well, but thanks for reviewing.

    – Python
    Dec 31 '18 at 12:56











  • Always include as little code as possible, yet as much as necessary to understand the problem. Have a look at How to create a Minimal, Complete, and Verifiable example :-) Just don't give up and keep improving step by step. Good luck!

    – finefoot
    Dec 31 '18 at 13:03



















  • I'm sorry, I think this question is too broad: Do you want help with saving the output? Reading from your saved output? Sending the mail? Those are all separate problems I'm afraid.

    – finefoot
    Dec 31 '18 at 12:46











  • Hi, i want read from saved output in this case, it is output.txt file which has above output starts from 3435 from the second line, i want to read that and send mail to user if that output exists, if no output in the second line do nothing

    – Python
    Dec 31 '18 at 12:48








  • 1





    This is just my personal feedback to your question: You should try to divide your problem in smaller parts. And then solve those small parts. Sending the mail has nothing to do with reading from a text file. It's irrelevant and distracts from the solution. You'll get much clearer questions and better answers here on StackOverflow. :-)

    – finefoot
    Dec 31 '18 at 12:51











  • ohh okie, i got your feedback now, sure thanks for the comments, sure i will do it. Actually i thought it will be included in the same code so added that part here as well, but thanks for reviewing.

    – Python
    Dec 31 '18 at 12:56











  • Always include as little code as possible, yet as much as necessary to understand the problem. Have a look at How to create a Minimal, Complete, and Verifiable example :-) Just don't give up and keep improving step by step. Good luck!

    – finefoot
    Dec 31 '18 at 13:03

















I'm sorry, I think this question is too broad: Do you want help with saving the output? Reading from your saved output? Sending the mail? Those are all separate problems I'm afraid.

– finefoot
Dec 31 '18 at 12:46





I'm sorry, I think this question is too broad: Do you want help with saving the output? Reading from your saved output? Sending the mail? Those are all separate problems I'm afraid.

– finefoot
Dec 31 '18 at 12:46













Hi, i want read from saved output in this case, it is output.txt file which has above output starts from 3435 from the second line, i want to read that and send mail to user if that output exists, if no output in the second line do nothing

– Python
Dec 31 '18 at 12:48







Hi, i want read from saved output in this case, it is output.txt file which has above output starts from 3435 from the second line, i want to read that and send mail to user if that output exists, if no output in the second line do nothing

– Python
Dec 31 '18 at 12:48






1




1





This is just my personal feedback to your question: You should try to divide your problem in smaller parts. And then solve those small parts. Sending the mail has nothing to do with reading from a text file. It's irrelevant and distracts from the solution. You'll get much clearer questions and better answers here on StackOverflow. :-)

– finefoot
Dec 31 '18 at 12:51





This is just my personal feedback to your question: You should try to divide your problem in smaller parts. And then solve those small parts. Sending the mail has nothing to do with reading from a text file. It's irrelevant and distracts from the solution. You'll get much clearer questions and better answers here on StackOverflow. :-)

– finefoot
Dec 31 '18 at 12:51













ohh okie, i got your feedback now, sure thanks for the comments, sure i will do it. Actually i thought it will be included in the same code so added that part here as well, but thanks for reviewing.

– Python
Dec 31 '18 at 12:56





ohh okie, i got your feedback now, sure thanks for the comments, sure i will do it. Actually i thought it will be included in the same code so added that part here as well, but thanks for reviewing.

– Python
Dec 31 '18 at 12:56













Always include as little code as possible, yet as much as necessary to understand the problem. Have a look at How to create a Minimal, Complete, and Verifiable example :-) Just don't give up and keep improving step by step. Good luck!

– finefoot
Dec 31 '18 at 13:03





Always include as little code as possible, yet as much as necessary to understand the problem. Have a look at How to create a Minimal, Complete, and Verifiable example :-) Just don't give up and keep improving step by step. Good luck!

– finefoot
Dec 31 '18 at 13:03












2 Answers
2






active

oldest

votes


















2














you can use it like this:



lines = output.split('n')


Now you can access lines by index e.g. lines[1]
or lines[1:].



You need to remove > output.txt from your command to get the output in output variable instead of output.txt file






share|improve this answer


























  • you can remove > output.txt from command as you are getting output in variable no need to save it in file and read again.

    – Amit Nanaware
    Dec 31 '18 at 12:50













  • Thanks.. I have updated the answer :)

    – Amit Nanaware
    Dec 31 '18 at 12:55











  • Thanks Amit, i will remove output.txt and try the code.

    – Python
    Dec 31 '18 at 13:44



















1














You can create list of lines from a file like this:



with open('output.txt') as input_file:
lines_list = input_file.readlines()
if len(lines_list) > 1:
lines_list=lines_list[1:] # drop the first line
send_mail()
else:
print ("else condition")


you open a file for reading using "r" and you build a list of lines. if the list is longer then 1, you build a new list by dropping the first line






share|improve this answer


























  • thanks @usr2564301

    – Yakov Dan
    Dec 31 '18 at 13:01











  • Thanks Yakov and usr2564301, so the above code means i do not have to give fp.close() explicitly? thanks in advance

    – Python
    Dec 31 '18 at 13:45











  • That's correct. It is also better, because sometimes you forget to close the file.

    – Yakov Dan
    Dec 31 '18 at 13:51











  • ok great, also at the last of your code, if i want to add one else condition like if if len(lines_list) > 1: lines_list=lines_list[1:] # drop the first line print "output exits", lines_list else: print "No output found" as i want to print this else condition when no output received in the second line

    – Python
    Dec 31 '18 at 14:07













  • yes, please see the edit of my answer

    – Yakov Dan
    Dec 31 '18 at 14:42













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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53987653%2fread-a-text-file-contents-from-second-line-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














you can use it like this:



lines = output.split('n')


Now you can access lines by index e.g. lines[1]
or lines[1:].



You need to remove > output.txt from your command to get the output in output variable instead of output.txt file






share|improve this answer


























  • you can remove > output.txt from command as you are getting output in variable no need to save it in file and read again.

    – Amit Nanaware
    Dec 31 '18 at 12:50













  • Thanks.. I have updated the answer :)

    – Amit Nanaware
    Dec 31 '18 at 12:55











  • Thanks Amit, i will remove output.txt and try the code.

    – Python
    Dec 31 '18 at 13:44
















2














you can use it like this:



lines = output.split('n')


Now you can access lines by index e.g. lines[1]
or lines[1:].



You need to remove > output.txt from your command to get the output in output variable instead of output.txt file






share|improve this answer


























  • you can remove > output.txt from command as you are getting output in variable no need to save it in file and read again.

    – Amit Nanaware
    Dec 31 '18 at 12:50













  • Thanks.. I have updated the answer :)

    – Amit Nanaware
    Dec 31 '18 at 12:55











  • Thanks Amit, i will remove output.txt and try the code.

    – Python
    Dec 31 '18 at 13:44














2












2








2







you can use it like this:



lines = output.split('n')


Now you can access lines by index e.g. lines[1]
or lines[1:].



You need to remove > output.txt from your command to get the output in output variable instead of output.txt file






share|improve this answer















you can use it like this:



lines = output.split('n')


Now you can access lines by index e.g. lines[1]
or lines[1:].



You need to remove > output.txt from your command to get the output in output variable instead of output.txt file







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 31 '18 at 12:54

























answered Dec 31 '18 at 12:48









Amit NanawareAmit Nanaware

826110




826110













  • you can remove > output.txt from command as you are getting output in variable no need to save it in file and read again.

    – Amit Nanaware
    Dec 31 '18 at 12:50













  • Thanks.. I have updated the answer :)

    – Amit Nanaware
    Dec 31 '18 at 12:55











  • Thanks Amit, i will remove output.txt and try the code.

    – Python
    Dec 31 '18 at 13:44



















  • you can remove > output.txt from command as you are getting output in variable no need to save it in file and read again.

    – Amit Nanaware
    Dec 31 '18 at 12:50













  • Thanks.. I have updated the answer :)

    – Amit Nanaware
    Dec 31 '18 at 12:55











  • Thanks Amit, i will remove output.txt and try the code.

    – Python
    Dec 31 '18 at 13:44

















you can remove > output.txt from command as you are getting output in variable no need to save it in file and read again.

– Amit Nanaware
Dec 31 '18 at 12:50







you can remove > output.txt from command as you are getting output in variable no need to save it in file and read again.

– Amit Nanaware
Dec 31 '18 at 12:50















Thanks.. I have updated the answer :)

– Amit Nanaware
Dec 31 '18 at 12:55





Thanks.. I have updated the answer :)

– Amit Nanaware
Dec 31 '18 at 12:55













Thanks Amit, i will remove output.txt and try the code.

– Python
Dec 31 '18 at 13:44





Thanks Amit, i will remove output.txt and try the code.

– Python
Dec 31 '18 at 13:44













1














You can create list of lines from a file like this:



with open('output.txt') as input_file:
lines_list = input_file.readlines()
if len(lines_list) > 1:
lines_list=lines_list[1:] # drop the first line
send_mail()
else:
print ("else condition")


you open a file for reading using "r" and you build a list of lines. if the list is longer then 1, you build a new list by dropping the first line






share|improve this answer


























  • thanks @usr2564301

    – Yakov Dan
    Dec 31 '18 at 13:01











  • Thanks Yakov and usr2564301, so the above code means i do not have to give fp.close() explicitly? thanks in advance

    – Python
    Dec 31 '18 at 13:45











  • That's correct. It is also better, because sometimes you forget to close the file.

    – Yakov Dan
    Dec 31 '18 at 13:51











  • ok great, also at the last of your code, if i want to add one else condition like if if len(lines_list) > 1: lines_list=lines_list[1:] # drop the first line print "output exits", lines_list else: print "No output found" as i want to print this else condition when no output received in the second line

    – Python
    Dec 31 '18 at 14:07













  • yes, please see the edit of my answer

    – Yakov Dan
    Dec 31 '18 at 14:42


















1














You can create list of lines from a file like this:



with open('output.txt') as input_file:
lines_list = input_file.readlines()
if len(lines_list) > 1:
lines_list=lines_list[1:] # drop the first line
send_mail()
else:
print ("else condition")


you open a file for reading using "r" and you build a list of lines. if the list is longer then 1, you build a new list by dropping the first line






share|improve this answer


























  • thanks @usr2564301

    – Yakov Dan
    Dec 31 '18 at 13:01











  • Thanks Yakov and usr2564301, so the above code means i do not have to give fp.close() explicitly? thanks in advance

    – Python
    Dec 31 '18 at 13:45











  • That's correct. It is also better, because sometimes you forget to close the file.

    – Yakov Dan
    Dec 31 '18 at 13:51











  • ok great, also at the last of your code, if i want to add one else condition like if if len(lines_list) > 1: lines_list=lines_list[1:] # drop the first line print "output exits", lines_list else: print "No output found" as i want to print this else condition when no output received in the second line

    – Python
    Dec 31 '18 at 14:07













  • yes, please see the edit of my answer

    – Yakov Dan
    Dec 31 '18 at 14:42
















1












1








1







You can create list of lines from a file like this:



with open('output.txt') as input_file:
lines_list = input_file.readlines()
if len(lines_list) > 1:
lines_list=lines_list[1:] # drop the first line
send_mail()
else:
print ("else condition")


you open a file for reading using "r" and you build a list of lines. if the list is longer then 1, you build a new list by dropping the first line






share|improve this answer















You can create list of lines from a file like this:



with open('output.txt') as input_file:
lines_list = input_file.readlines()
if len(lines_list) > 1:
lines_list=lines_list[1:] # drop the first line
send_mail()
else:
print ("else condition")


you open a file for reading using "r" and you build a list of lines. if the list is longer then 1, you build a new list by dropping the first line







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 31 '18 at 14:41

























answered Dec 31 '18 at 12:48









Yakov DanYakov Dan

1,386717




1,386717













  • thanks @usr2564301

    – Yakov Dan
    Dec 31 '18 at 13:01











  • Thanks Yakov and usr2564301, so the above code means i do not have to give fp.close() explicitly? thanks in advance

    – Python
    Dec 31 '18 at 13:45











  • That's correct. It is also better, because sometimes you forget to close the file.

    – Yakov Dan
    Dec 31 '18 at 13:51











  • ok great, also at the last of your code, if i want to add one else condition like if if len(lines_list) > 1: lines_list=lines_list[1:] # drop the first line print "output exits", lines_list else: print "No output found" as i want to print this else condition when no output received in the second line

    – Python
    Dec 31 '18 at 14:07













  • yes, please see the edit of my answer

    – Yakov Dan
    Dec 31 '18 at 14:42





















  • thanks @usr2564301

    – Yakov Dan
    Dec 31 '18 at 13:01











  • Thanks Yakov and usr2564301, so the above code means i do not have to give fp.close() explicitly? thanks in advance

    – Python
    Dec 31 '18 at 13:45











  • That's correct. It is also better, because sometimes you forget to close the file.

    – Yakov Dan
    Dec 31 '18 at 13:51











  • ok great, also at the last of your code, if i want to add one else condition like if if len(lines_list) > 1: lines_list=lines_list[1:] # drop the first line print "output exits", lines_list else: print "No output found" as i want to print this else condition when no output received in the second line

    – Python
    Dec 31 '18 at 14:07













  • yes, please see the edit of my answer

    – Yakov Dan
    Dec 31 '18 at 14:42



















thanks @usr2564301

– Yakov Dan
Dec 31 '18 at 13:01





thanks @usr2564301

– Yakov Dan
Dec 31 '18 at 13:01













Thanks Yakov and usr2564301, so the above code means i do not have to give fp.close() explicitly? thanks in advance

– Python
Dec 31 '18 at 13:45





Thanks Yakov and usr2564301, so the above code means i do not have to give fp.close() explicitly? thanks in advance

– Python
Dec 31 '18 at 13:45













That's correct. It is also better, because sometimes you forget to close the file.

– Yakov Dan
Dec 31 '18 at 13:51





That's correct. It is also better, because sometimes you forget to close the file.

– Yakov Dan
Dec 31 '18 at 13:51













ok great, also at the last of your code, if i want to add one else condition like if if len(lines_list) > 1: lines_list=lines_list[1:] # drop the first line print "output exits", lines_list else: print "No output found" as i want to print this else condition when no output received in the second line

– Python
Dec 31 '18 at 14:07







ok great, also at the last of your code, if i want to add one else condition like if if len(lines_list) > 1: lines_list=lines_list[1:] # drop the first line print "output exits", lines_list else: print "No output found" as i want to print this else condition when no output received in the second line

– Python
Dec 31 '18 at 14:07















yes, please see the edit of my answer

– Yakov Dan
Dec 31 '18 at 14:42







yes, please see the edit of my answer

– Yakov Dan
Dec 31 '18 at 14:42




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53987653%2fread-a-text-file-contents-from-second-line-in-python%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'