Home.Verilog.Digital Design.Digital Basics.Python.RF Basics.
Previous.
Next.
Custom Search

Feedback ? Send it to admin@fullchipdesign.com or join me at fullchip@gmail.com

Legal Disclaimer

Chip Designing for ASIC/ FPGA Design engineers and Students
FULLCHIPDESIGN
Digital-logic Design...  Dream for many students… start learning front-end…

Legal Disclaimer

PICS
Verilog Tutorial.
File read operations.

Class & global var.

File write op's.

Glob module.

Conditional stms.

String 2 hex.

UNIX in Python.

Diamond Pattern.

TypeError - arg.

TyprError - str.

TypeError - strconv.

AttributeError.

Function call.

Read bin files.

Program to open and write file

This program writes to the file specified in the python code

import sys

import os

import string

def write_file(file):

     file_to_write = open('test_write.txt', 'w')

     file_to_write.write('Text to write')

     file_to_write.close()

file = write_file(open('test_write.txt', 'w'))

Now lets make the file writes to be more interesting

Specify files to be written from the command line

Code to write into files specified from the command line

#!/usr/bin/python

import sys

import os

import string

def write_file(file):

     file_to_write = file

     file_to_write.write('Text to write')

     file_to_write.close()

     return

if len(sys.argv) == 1:

    write_file(sys.stdin)

else:

    write_file(open(sys.argv[1], 'w'))

File writes using classes and global variables

We can further modify the writes by using classes and global variables in python

import sys

import os

import string

class writefile:

    def write_file(self, file):

         self.file_to_write = file

         self.file_to_write.write('Text to write')

         self.file_to_write.close()

         return

if len(sys.argv) == 1:

    r1 = writefile()

    r1.write_file(sys.stdin)

else:

    r1 = writefile()

    r1.write_file(open(sys.argv[1], 'w'))

Python file write operations