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

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

@TYH :- 4G LTE Long Term Evolution Tutorial, CloudComputing
PICS
Verilog Tutorial.
Get Noticed:- Submit your own content to be published on fullchipdesign.com

Send it to fullchip@gmail.com

Digital Basics 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 in Python to open and write file

This program writes to the file specified with-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

Python code to write into files which are 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'))

Python File writes using classes & global variables

We can further modify the Python file 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

LTE - Long Term Evolution topics from here