python save output of shell command to text file


BoJack Knight

I would like to save the output of a shell command to a text file via Python. Here is my actual, very basic python code:

EDIT Here is the final script, thanks for your help :)

import subprocess

ip_adress_4 = 0    
pr = open("pointer_record.txt", "w")

while (ip_adress_4 < 255):
    ip_adress_4 = ip_adress_4 + 1
    ip_adress = '82.198.205.%d' % (ip_adress_4,)
    subprocess.Popen("host %s" % ip_adress, stdout=pr, shell=True)
Raydel Miranda

Try something like this:

import subprocess
file_ = open("ouput.txt", "w")
subprocess.Popen("ls", stdout=file_)

Edit : for your needs

import subprocess

file_ = open("ouput.txt", "w")
subprocess.Popen(["host", ipAddress], stdout=file_)

Related


ssh command output to save in text file in shell script

Mars I want to write shell script in which I use ssh command. Whatever output I get via the ssh command, I want to save it in a text file or varibale so I can use it in my shell script. Currently, I save the output in a variable, but when I use the variable ou

How to save os command output in a text file?

Dhananjay I'm trying to write a script that takes os commands (linux) and saves it in a text file. However, when I try to run this code, the output of the os command is not saved in the text file. #!/usr/bin/python import sys import os target = raw_input('En

Save the output of the command line in a text file?

username I am using the following command line to analyze the data: unpackdcm -scr ${in} -targ ${out} The command is printing the status on the screen along with a description of the progress of the job. To save the state I did the following: unpackdcm -scr

Save the output of the command line in a text file?

username I am using the following command line to analyze the data: unpackdcm -scr ${in} -targ ${out} The command is printing the status on the screen along with a description of the progress of the job. To save the state I did the following: unpackdcm -scr

How to save python screen output to text file

Iron Fist I am new to Python. I need to query an item from a dictionary and save the result to a text file. Here is what I have: import json import exec.fullog as e input = e.getdata() #input now is a dict() which has items, keys and values. #Query print 'D

Spark with Python: save RDD output to text file

RACHITA PATRO I am trying to do a word count problem in spark using python. However, when I try to save the output RDD in a text file using the .saveAsTextFile command, I run into a problem. Here is my code. please help me. I'm stuck. thank you for your time.

How to save python output to text file

Nguyen Duc Viet I have to save the result to a txt file but don't know anything about it. I desperately need some of you to help me. I am truely thankful. my code: import pandas as pd import numpy as np import glob filenames = glob.glob('E:\koppen-master\da

How to save python output to text file

Nguyen Duc Viet I have to save the result to a txt file but don't know anything about it. I desperately need some of you to help me. I am truely thankful. my code: import pandas as pd import numpy as np import glob filenames = glob.glob('E:\koppen-master\da

Spark with Python: save RDD output to text file

RACHITA PATRO I am trying to do a word count problem in spark using python. However, when I try to save the output RDD in a text file using the .saveAsTextFile command, I run into a problem. Here is my code. please help me. I'm stuck. thank you for your time.

How to save python output to text file

Nguyen Duc Viet I have to save the result to a txt file but don't know anything about it. I desperately need some of you to help me. I am truely thankful. my code: import pandas as pd import numpy as np import glob filenames = glob.glob('E:\koppen-master\da

How to save python screen output to text file

Iron Fist I am new to Python. I need to query an item from a dictionary and save the result to a text file. Here is what I have: import json import exec.fullog as e input = e.getdata() #input now is a dict() which has items, keys and values. #Query print 'D

Python script needs to save output to text file

K. Melio I gathered some code from the internet to capture the key pressed and the current active window title and am trying to write the output of a python script to a text file. The script works fine in the IDLE console and prints out the keys pressed and lo

Spark with Python: save RDD output to text file

RACHITA PATRO I am trying to do a word count problem in spark using python. However, when I try to save the output RDD in a text file using the .saveAsTextFile command, I run into a problem. Here is my code. please help me. I'm stuck. thank you for your time.

Python: How to save generator output to text file?

username I am using the following generator to calculate moving average: import itertools from collections import deque def moving_average(iterable, n=50): it = iter(iterable) d = deque(itertools.islice(it, n-1)) d.appendleft(0) s = sum(d)

How to save python output to text file

Nguyen Duc Viet I have to save the result to a txt file but don't know anything about it. I desperately need some of you to help me. I am truely thankful. my code: import pandas as pd import numpy as np import glob filenames = glob.glob('E:\koppen-master\da

How to save python screen output to text file

Iron Fist I am new to Python. I need to query an item from a dictionary and save the result to a text file. Here is what I have: import json import exec.fullog as e input = e.getdata() #input now is a dict() which has items, keys and values. #Query print 'D

Python: How to save output to text file?

Diego 1.我尝试使用-o werwe wer we wer we we and 2.and >> text.txtrr rrrrrrrrrrrrrrrrrrrrrrrrrr # -*- coding: iso-8859-15 import sys import requests import os if len(sys.argv) >= 2: os.system('clear') print "Please wait..

How to save python output to text file

Nguyen Duc Viet I have to save the result to a txt file but don't know anything about it. I desperately need some of you to help me. I am truely thankful. my code: import pandas as pd import numpy as np import glob filenames = glob.glob('E:\koppen-master\da

How to save python output to text file

Nguyen Duc Viet I have to save the result to a txt file but don't know anything about it. I desperately need some of you to help me. I am truely thankful. my code: import pandas as pd import numpy as np import glob filenames = glob.glob('E:\koppen-master\da

How to save python screen output to text file

Iron Fist I am new to Python. I need to query an item from a dictionary and save the result to a text file. Here is what I have: import json import exec.fullog as e input = e.getdata() #input now is a dict() which has items, keys and values. #Query print 'D

Spark with Python: save RDD output to text file

RACHITA PATRO I am trying to do a word count problem in spark using python. However, when I try to save the output RDD in a text file using the .saveAsTextFile command, I run into a problem. Here is my code. Please help me. I'm stuck. thank you for your time.

Spark with Python: save RDD output to text file

RACHITA PATRO I am trying to do a word count problem in spark using python. However, when I try to save the output RDD in a text file using the .saveAsTextFile command, I run into a problem. Here is my code. Please help me. I'm stuck. thank you for your time.

Python script needs to save output to text file

K. Melio I gathered some code from the internet to capture the key pressed and the current active window title and am trying to write the output of a python script to a text file. The script works fine in the IDLE console and prints out the keys pressed and lo

How to save python output to text file

Nguyen Duc Viet I have to save the result to a txt file but don't know anything about it. I desperately need some of you to help me. I am truely thankful. my code: import pandas as pd import numpy as np import glob filenames = glob.glob('E:\koppen-master\da

How to save python output to text file

Nguyen Duc Viet I have to save the result to a txt file but don't know anything about it. I desperately need some of you to help me. I am truely thankful. my code: import pandas as pd import numpy as np import glob filenames = glob.glob('E:\koppen-master\da

Python: How to save generator output to text file?

username I am using the following generator to calculate moving average: import itertools from collections import deque def moving_average(iterable, n=50): it = iter(iterable) d = deque(itertools.islice(it, n-1)) d.appendleft(0) s = sum(d)

How to save the output to a text file in python?

Misha Eide So what I'm trying to do is save the output of that program to a text file. import itertools res = itertools.product('qwertyuiopasdfghjklzxcvbnm', repeat=3) for i in res: print ''.join(i) I am running python 2.7 Will You can use openand then use w

How to write Python Shell to output text file?

Slash x43 I need to write Python Shell to an output text file. I have written some of them to the output text file, but now I need to add the number of lines and the numbers in each line to my output text file. I am trying to add another for loop outside the f

shell output to text file

papaya So I have the following shell: #!/bin/bash for (( i = 1; i <= 6; i++ )) do printf in_port=$i,actions= for (( j = 1 ; j <= 6; j++ )) do if [ $i != $j ]; then printf output:$j, fi done printf "\n" do