creating separate output file for each input file in python


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Programming creating separate output file for each input file in python
# 1  
creating separate output file for each input file in python

Experts,
Need your help for this. Please support

My motive is to create seperate output file for each Input Files(File 1 and File2) in another folder say(/tmp/finaloutput)

Input files

File 1(1.1.1.1.csv)
Code:
a,b,c
43,17104773,3
45,17104234,4

File 2(2.2.2.2.csv)
Code:
a,b,c
43,17104773,1
45,17104234,2

I am greping column 3 values from File1 and File2 and trying to use this column3 values in below code for this command :

Code:
stdin, stdout, stderr = open_channel.exec_command("show run interface gigabitEthernet %s\r\n" %data)

My motive is to create seperate output file for each Input Files(File 1 and Fil2) in another folder

Below is File where all node Ip's are placed to be used in below script for logging into node

Code:
IP File(testin.csv)
1.1.1.1
2.2.2.2

So first i have to login to node with IP(1.1.1.1) and then have to grep File1(1.1.1.1.csv) and will grep File 1 3rd column value(3,4) and want to pass values one by one to above commands and need to store data generated(from the 3rd column values 3 and 4) in output file with same name(1.1.1.1.csv) in another folder say(/tmp/finaloutput)

Again have to login to node with IP(2.2.2.2) and have to grep File2(2.2.2.2.csv) and will grep File 2 3rd column value(1,2) and want to paas values one by one to above commands and need to store data generated in output file with same name(2.2.2.2.csv) in another folder say(/tmp/finaloutput)


My code :

Code:
#!/usr/bin/env python
import sys
import telnetlib
import os
import subprocess
import paramiko
import csv
import os
import logging
open_channel = paramiko.SSHClient()

# This is the main logger object to be used throughout the script
logger = None

# Create the necessary log handles
#   A log file is created with the filename specified
#   Other log messages are also printed on the console as per the level
def createLogHandlers(logfile):
    global logger
    logging.basicConfig(filename=logfile ,
                        filemode='a',
                        level=logging.DEBUG,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%a, %d %b %Y %H:%M:%S')
    logger = logging.getLogger('Telnet')

    # Create handler which logs messages on console
    log_console = logging.StreamHandler()
    log_console.setLevel(logging.DEBUG)
    log_console.setFormatter(logging.Formatter('%(asctime)s %(levelname)-8s %(message)s',
                                                datefmt='%a, %d %b %Y %H:%M:%S'))
    logger.addHandler(log_console)


# Global Objects
COLUMN_DATA = []
PATHSTR = "/tmp/input"
os.chdir (PATHSTR)
#computer_list = open("/tmp/testin.csv", 'r')
#i = []
'''
This will extract data from all the files with filename
pattern "aa<number>" present in the PATHSTR
'''
def extract_column_data():
    global COLUMN_DATA


    for filename in os.listdir(PATHSTR):
        if filename.endswith(".csv"):
            f  = open(filename)
            next(f)
            reader = csv.reader(f, delimiter=",")

            for row in reader:
                print row[2]
                COLUMN_DATA.append(row[2])
                x=filename.rsplit('.',1)[0]
                f1=open(x+str('_v1.txt'),"w")
                f1.write(str(COLUMN_DATA))
                f1.close()
                continue
        else:
                continue

def my_function(i):
    global COLUMN_DATA

    for data in COLUMN_DATA:
        saved_ip = open('/tmp/outputcommand'"%s.txt"%i, "a+")
        stdin, stdout, stderr = open_channel.exec_command("show run interface gigabitEthernet %s\r\n" %data)
        output = stdout.readlines()
        output = "". join(output)
        print(output)
        saved_ip.write(output)
        saved_ip.close()




### MAIN FUNCTION ###

if __name__ == "__main__":
    createLogHandlers('telnet_a.log')
    extract_column_data()
    computer_list = open("/tmp/testin.csv", 'r')
    for i in computer_list.readlines():
        open_channel = paramiko.SSHClient()
        open_channel.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        open_channel.connect(i, port = 22, username = 'abc',
                             password = 'abc')
    my_function(i)


Error :

Code:
Traceback (most recent call last):
  File "./telnet3bkpbkp3107.py", line 94, in <module>
    my_function(i)
  File "./telnet3bkpbkp3107.py", line 72, in my_function
    stdin, stdout, stderr = open_channel.exec_command("show run interface gigabitEthernet %s\r\n" %data)
  File "/usr/lib/python2.6/site-packages/paramiko/client.py", line 349, in exec_command
    chan = self._transport.open_session()
  File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 648, in open_session
    return self.open_channel('session')
  File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 738, in open_channel
    raise e
EOFError


Only able to get the below data in one file, only for One value(3) for File 1.

Code:
Building configuration...

Current configuration : 107 bytes
!
interface GigabitEthernet3
 no ip address
 shutdown
 negotiation auto
 no mop enabled
 no mop sysid
end

# 2  
I don't think saved_ip = open('/tmp/outputcommand'"%s.txt"%i, "a+") does what you think it does. What are %s and %i supposed to be?
# 3  
Hi Corona688,

%s will put the device IP after "outputcommand" to which we have logged in to collect data
%i is the i argument passed into function(my function). I contains list of Ip's taken from file (testin.csv)
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #268
Difficulty: Easy
In 1936, Turing published his paper 'On Computable Numbers, with an Application to the Entscheidungsproblem'. In this paper, Turing reformulated Kurt Godel's 1931 results on the limits of proof and computation, replacing Godel's universal arithmetic-based formal language with the formal and simple hypothetical devices that became known as Turing machines.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Creating a master file of conjugated verbs by concatenating root and inflection from separate files

Excuses for the long descriptive title. I am working with Sindhi and developing a database of all verbal conjugations in that language. I have generated 2 files: Verbs.dic contains all the verbs, one verb per line Inflections.dic contains the verbal conjugations which need to be appended to... (6 Replies)
Discussion started by: gimley
6 Replies

3. Shell Programming and Scripting

Trying to use diff output to compare to a separate file

I have two files: smw:/working/iso_testing # cat a QConvergeConsoleCLI-1.1.03-49.x86_64.rpm aaa_base-13.2+git20140911.61c1681-1.3.i586.rpm acpica-20140724-2.1.2.i586.rpm test.rpm smw:/working/iso_testing # cat b QConvergeConsoleCLI-1.1.03-49.x86_64.rpm... (12 Replies)
Discussion started by: jedlund21
12 Replies

4. Shell Programming and Scripting

Creating a file from input stream

Hi, Need some help with creating a file from input steam. Meaning from following command myfunc should be able to store the input stream to a file. echo a b c | myfunc The file thus created should have - a b c Here's what I've tried in myfunc() but didn't help - myfunc() { cat... (3 Replies)
Discussion started by: nexional
3 Replies

5. Shell Programming and Scripting

Separate Text File into Two Lists Using Python

Hello, I have a pretty simple question, but I am new to Python and am trying to write a simple program. Put simply, I want to take a text file that looks like this: 11111 22222 33333 44444 55555 66666 77777 88888 and produce two lists, one containing the contents of the left column, one the... (0 Replies)
Discussion started by: Tyler_92
0 Replies

6. Shell Programming and Scripting

extract DDL - output every match to separate file

Hi, i want to extract the 'CREATE INDEX' or 'CREATE UNIQUE INDEX' statements from a ddl file and output each match to a separate file. i was looking around the net but couldnīt find anything. a possible sed-script could be: sed -n '/CREATE*INDEX*/,/COMMIT/p' filename.ddlbut i couldnīt find out... (11 Replies)
Discussion started by: CactusMoon
11 Replies

7. UNIX for Dummies Questions & Answers

creating separate directories according to file extension and keeping file in different directory as

unix program to which a directory name will be passed as parameter. This directory will contain files with various extensions. This script will create directories with the names of the extention of the files and then put the files in the corresponding folder. All files which do not have any... (2 Replies)
Discussion started by: Deekay.p
2 Replies

8. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

9. Shell Programming and Scripting

creating files and getting input from another file

I have a file where i have files name with absolute path. Ex: files.dat and contents are: /root/xy/yz/zz/abc.txt /root/xx/yy/zz/ac.txt /root/xz/yz/zx/bc.txt /root/xy/yz/zx/abcd.txt now i want to create all above files(dummy files and can be 0 byte). i thought of using touch but it doesn't... (10 Replies)
Discussion started by: ajayyadavmca
10 Replies

10. UNIX for Dummies Questions & Answers

Output of command to 2 separate file?

How would I echo the same results to two files? One is a running log, and the other is a cache, or sort. echo "Hello World" >> /Logs/File1 & /tmp/file2 I would just copy it from one place to the other, but the Log keeps history, where I want the /tmp to hold only stuff per session. I tried... (2 Replies)
Discussion started by: TheCrunge
2 Replies

Featured Tech Videos