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

~\Documents\fullchip\python\mysite\webpages\templates\webpages\test.py.html
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

LTE - 4G Wireless Technology

Digital fundamentals.

Interview Questions.

Input TEST vectors ‘to_try_vectors.dat’

0.955511 1.212213

-1.450977 0.958842

RESULTS output file input_python_file_c.dat

0.955511

1.212213

-1.450977

0.978842

Tutorials @fullchipdesign.com

Verilog Tutorial.

LTE Tutorial.

Memory Tutorial.

Following list of programs are discussed in this sec:

¶ File Operations: 

  1. Open and read file from within the python program as a single string. Command line arguments lists as string with ‘sys.argv’.
  2. Use of  glob module in Python code . Read file using classes, global variables and functions. Program to open and write file.
  3. Specify files to be written from the command line. File writes using classes and global variables

¶ Program to read file, filter text, conversion to HEX, conversion from Hexadecimal to Signed Magnitude.S Steps for conditional statements, converting string to hexadecimalvalue, strip of white space at end, converting 12 bit hexadecimal values to 12 bit signed values.

¶ Advanced python code to cover UNIX shell commands, read lines from a text file, ‘sysargv’ command line operations, use of class and multiple functions. 

¶ Write a program to generate diamond pattern.

¶ Implement Python function calls to different classes.

¶ Python read binary files, use of struct.unpack.

¶ Python error’s with complete code examples:-

  1. TypeError: __init__() takes exactly 2 arguments (1 given),
  2. TypeError: 'str' object is not callable, TypeError: not all arguments converted during string formatting,
  3. AttributeError: ** instance has no attribute **

 

 

Hope you liked! this page. Don't forgot to access relevant previous and next sections with links below.