Advanced Python code for running UNIX commands and File Operations.
Advanced Python code for running UNIX commands and File Operations
¨ Running UNIX shell commands from within the python code.
¨ Open and read file from command file
¨ Read all lines in text file and execute a set of functions for each line.
¨ Open and write file from the command line.
Python Program rw_test1.py
import sys import os import string import commands ### Author:- FullChipDesign ### ### Description :- Utility for running UNIX commands and file operations ### class file_op: def file_read(self, file): self.file_to_read = file self.filn = 0 # UNIX command line operations commands.getoutput('rm -rf ./input_python_file_c.dat') # Read all lines in a text file and execute the following for loop for each line for self.filn in self.file_to_read.readlines(): self.rfile = self.filn.rstrip('\n\r\t') # Calculate length of each line self.ln = len(self.rfile) # print self.rfile self.filter_text() # self.string_to_signed() # Strip off white space on end self.rfile = string.strip(self.rfile) return # Function for data manuplation # File write operations def filter_text (self): self.ci = 0 self.idata = 0 self.qdata = 0 self.test = 0 self.temp_write = open('input_python_file_c.dat' , 'a') for self.ci in range(0, 1, 1): self.idata = self.rfile[self.ci:self.ci+8] self.qdata = self.rfile[self.ci+9:self.ci+18] # print self.idata # print self.qdata self.idata = string.strip(self.idata) self.qdata = string.strip(self.qdata) # Write file self.temp_write.write("%s \n" % self.idata) self.temp_write.write("%s \n" % self.qdata) # Close file self.temp_write.close() return # Command line operations for reading file from command line if len(sys.argv) == 1: r1 = file_op() r1.file_read(sys.stdin) else: r1 = file_op() r1.file_read(open